Skip to content

Commit

Permalink
fix issues #85 and #98
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpdg committed Dec 7, 2021
1 parent 0d540fc commit c60af34
Show file tree
Hide file tree
Showing 33 changed files with 15,846 additions and 14,366 deletions.
1 change: 1 addition & 0 deletions CIEID/CIEID.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Intro.Designer.cs">
<DependentUpon>Intro.cs</DependentUpon>
</Compile>
<Compile Include="Logger.cs" />
<Compile Include="MoveablePictureBox.cs">
<SubType>Component</SubType>
</Compile>
Expand Down
7,004 changes: 3,620 additions & 3,384 deletions CIEID/Form1.Designer.cs

Large diffs are not rendered by default.

365 changes: 260 additions & 105 deletions CIEID/Form1.cs

Large diffs are not rendered by default.

18,982 changes: 9,491 additions & 9,491 deletions CIEID/Form1.resx

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions CIEID/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.IO;

namespace CIEID
{
public enum LogLevel
{
NONE = 0,
DEBUG = 1,
INFO = 2,
ERROR = 3,
};

public class Logger
{
public static LogLevel DefaultLogLevel = LogLevel.NONE;
private LogLevel _level;
public LogLevel Level
{
get => _level;
set => _level = value;
}

// The default argument value should be `Logger.DefaultLogLevel`
// rather than an explicit LogLevel enum value.
public Logger(LogLevel logLevel = LogLevel.NONE)
{
Info($"Init della classe Logger, livello di log {logLevel}");
Level = logLevel;
}

// Public functions
public void Debug(string message)
{
Log($"[D] {message}", LogLevel.DEBUG);
}

public void Info(string message)
{
Log($"[I] {message}", LogLevel.INFO);
}

public void Error(string message)
{
Log($"[E] {message}", LogLevel.ERROR);
}

public void Log(string message, LogLevel messageLevel)
{
if (Level > 0 && Level <= messageLevel)
{
Write(message);
}
}

private void Write(string message)
{
string programDataDir = System.Environment.GetEnvironmentVariable("PROGRAMDATA");
string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
string logFilePath = $"{programDataDir}\\CIEPKI\\CIEID_{currentDate}.log";
try
{
StreamWriter streamWriter = new StreamWriter(logFilePath, append: true);
streamWriter.WriteLineAsync($"{timestamp} {message}");
streamWriter.Close();
}
catch(Exception)
{
Console.WriteLine($"{timestamp} {message}");
}
}
}
}
156 changes: 152 additions & 4 deletions CIEID/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* CIE ID, l'applicazione per gestire la CIE
* Author: Ugo Chirico - http://www.ugochirico.com
* Data: 10/04/2019
* Data: 10/04/2019
*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
Expand All @@ -17,9 +18,16 @@ namespace CIEID
static class Program
{
private static Mutex mutex = null;
private static string LogConfigPrefixLib ="LIB_LOG_LEVEL";
private static string LogConfigPrefixApp ="APP_LOG_LEVEL";
// Valori di default per il logger
public static LogLevel LogLevelApp = Logger.DefaultLogLevel;
public static LogLevel LogLevelLib = Logger.DefaultLogLevel;


[DllImport("user32.dll", SetLastError = true)]
static extern bool SetProcessDPIAware();
static public Logger Logger;

/// <summary>
/// Punto di ingresso principale dell'applicazione.
Expand All @@ -30,11 +38,18 @@ static void Main(string[] args)
const string appName = "CIEID";
bool createdNew;

Logger = new Logger(LogLevelApp);
LogLevel[] LogConfigFile = LoadLogConfigFromFile();
SetLogConfig(LogConfigFile[0], LogConfigFile[1]);
Logger.Info("App CIE - logger inizializzato");
Logger.Error($"Livello di log applicazione: {LogLevelApp}");
Logger.Error($"Livello di log libreria: {LogLevelLib}");

mutex = new Mutex(true, appName, out createdNew);

if (!createdNew)
{
//app is already running! Exiting the application
//app is already running! Exiting the application
return;
}

Expand All @@ -49,14 +64,147 @@ static void Main(string[] args)
//Properties.Settings.Default.Save();

if (Properties.Settings.Default.firstTime)
{
{
Application.Run(new Intro());
}
else
{
{
Application.Run(new MainForm(args.Length > 0 ? args[0] : null));
}

}

static LogLevel[] LoadLogConfigFromFile()
{
LogLevel logLevelApp = Logger.DefaultLogLevel;
LogLevel logLevelLib = Logger.DefaultLogLevel;
bool _writeLogConfigFile = false;

string programDataDir = System.Environment.GetEnvironmentVariable("PROGRAMDATA");
string configFilePath = $"{programDataDir}\\CIEPKI\\config";
try {
Console.WriteLine("Leggo file configurazione log");
string[] configFileLines = System.IO.File.ReadAllLines(configFilePath);
foreach (var line in configFileLines) {
if (line.Contains(LogConfigPrefixApp))
{
Console.WriteLine($"Configurazione log applicazione: {line}");
string value = line.Split('=')[1];
try
{
int intValue = int.Parse(value);
if (Enum.IsDefined(typeof(LogLevel), intValue))
{
logLevelApp = (LogLevel)intValue;
}
else
{
Console.WriteLine($"valore '{intValue}' del livello di log applicazione fuori intervallo - uso default");
_writeLogConfigFile = true;
}
}
catch (FormatException e)
{
Console.WriteLine($"valore '{value}' del livello di log applicazione non valido - uso default");
_writeLogConfigFile = true;
}
}
else if (line.Contains(LogConfigPrefixLib))
{
Console.WriteLine($"Configurazione log libreria: {line}");
string value = line.Split('=')[1];
try
{
int intValue = int.Parse(value);
if (Enum.IsDefined(typeof(LogLevel), intValue))
{
logLevelLib = (LogLevel)intValue;
}
else
{
Console.WriteLine($"valore '{intValue}' del livello di log libreria fuori intervallo - uso default");
_writeLogConfigFile = true;
}
}
catch (FormatException)
{
Console.WriteLine($"valore '{value}' del livello di log libreria non valido - uso default");
_writeLogConfigFile = true;
}
}
else
{
Console.WriteLine($"Riga di configurazione log non valida: {line}");
}
}
}

catch (FileNotFoundException)
{
Console.WriteLine($"File configurazione log non trovato, lo creo con valori di default");
_writeLogConfigFile = true;
}
catch (DirectoryNotFoundException)
{
Console.WriteLine($"Directory di configurazione log non trovata, la creo");
try
{
System.IO.Directory.CreateDirectory($"{programDataDir}\\CIEPKI");
_writeLogConfigFile = true;
}
catch (Exception)
{
Console.WriteLine($"Errore nel creare directory per il file di configurazione dei log");
_writeLogConfigFile = false;
}
}

if (_writeLogConfigFile)
{
SaveLogConfigToFile(logLevelApp, logLevelLib);
}

return new LogLevel[] {logLevelApp, logLevelLib};
}

public static void SetLogConfig(LogLevel logLevelApp, LogLevel logLevelLib)
{
Logger.Info("SetLogConfig() - Inizia funzione");
Logger.Debug($"SetLogConfig() - logLevelApp: {logLevelApp}");
Logger.Debug($"SetLogConfig() - logLevelLib: {logLevelLib}");
LogLevelApp = logLevelApp;
LogLevelLib = logLevelLib;
Logger.Level = LogLevelApp;
}

public static void SaveLogConfigToFile()
{
Logger.Info("SaveLogConfigToFile() - Inizia funzione");
SaveLogConfigToFile(LogLevelApp, LogLevelLib);
}

static void SaveLogConfigToFile(LogLevel logLevelApp, LogLevel logLevelLib)
{
Logger.Info("SaveLogConfigToFile(LogLevel, LogLevel) - Inizia funzione");
Logger.Debug($"SaveLogConfigToFile(LogLevel, LogLevel) - logLevelApp: {logLevelApp}");
Logger.Debug($"SaveLogConfigToFile(LogLevel, LogLevel) - logLevelLib: {logLevelLib}");
string programDataDir = System.Environment.GetEnvironmentVariable("PROGRAMDATA");
string configFilePath = $"{programDataDir}\\CIEPKI\\config";
string[] lines =
{
$"LIB_LOG_LEVEL={(int)logLevelLib}", $"APP_LOG_LEVEL={(int)logLevelApp}",
};
try
{
File.WriteAllLines(configFilePath, lines);
}
catch (Exception)
{
string message = $"Errore nel salvare il file di configurazione dei log";
Logger.Error(message);
Console.WriteLine(message);
}
}

}
}
2 changes: 1 addition & 1 deletion CIEID/SignerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public int verify()

int n_sign = (int)verificaConCIE(filePath, proxyAddress, proxyPort, proxyCredentials);

Console.WriteLine("n_sign: {0}", n_sign);
Console.WriteLine("n_sign: {0}", (UInt32) n_sign);
if (n_sign <= 0)
{
return n_sign;
Expand Down
Loading

0 comments on commit c60af34

Please sign in to comment.