/// <summary> /// Default constructor /// </summary> /// <param name="verbosity">Only messages higher than this will be logged</param> /// <param name="logPath">Path to log file on disk</param> /// <param name="timeFormat">RFC822 time format by default</param> public Logger(MessageVerbosity verbosity, string logPath, string timeFormat = "ddd, dd MMM yyyy HH:mm:ss K") { _verbosity = verbosity; _logPath = logPath; _timeFormat = timeFormat; _timeToShutdown = false; // check that log exists, or create it if (!File.Exists(_logPath)) { using (File.Create(_logPath)) { } // if the file couldn't be created (and _that_ didn't throw an exception), throw an exception if (!File.Exists(_logPath)) throw new Error.HelixException(Error.HelixExceptionSource.IoError, string.Format("Could not create log file: {0}", _logPath)); } // start the log thread Thread t = new Thread(LogThreadWork); t.Name = "Logging thread (" + _logPath + ")"; t.IsBackground = true; t.Start(); }
private static void InnerLog(string message, MessageVerbosity type, bool isInnerCall, System.Exception e = null) { if (Loaded) { switch (type) { case MessageVerbosity.Debug: Logger.Debug(message); break; case MessageVerbosity.Info: Logger.Info(message); break; case MessageVerbosity.Error: if (isInnerCall) { OnTracerError?.Invoke(message); } if (e != null) { Logger.Error(message, e); } else { Logger.Error(message); } break; } } else { OnTracerError?.Invoke("Tracer is not loaded"); } }
void singleInstance_ArgumentsReceived(object sender, ArgumentsReceivedEventArgs e) { // Using "/exit" anywhere in the command list will cause EasyImgur to exit after uploading; // this will happen regardless of the execution sending the exit command was the execution that // launched the initial instance of EasyImgur. bool exitWhenFinished = false; bool anonymous = false; // mappings of switch names to actions Dictionary <string, Action> handlers = new Dictionary <string, Action>() { { "anonymous", () => anonymous = true }, { "noinfo", () => Verbosity = (MessageVerbosity)Math.Max((int)Verbosity, (int)MessageVerbosity.NoInfo) }, { "q", () => Verbosity = (MessageVerbosity)Math.Max((int)Verbosity, (int)MessageVerbosity.NoInfo) }, { "noerr", () => Verbosity = (MessageVerbosity)Math.Max((int)Verbosity, (int)MessageVerbosity.NoError) }, { "qq", () => Verbosity = (MessageVerbosity)Math.Max((int)Verbosity, (int)MessageVerbosity.NoError) }, { "exit", () => exitWhenFinished = true }, { "portable", () => { } } // ignore }; try { // First scan for switches int badSwitchCount = 0; foreach (String str in e.Args.Where(s => s != null && s.StartsWith("/"))) { String param = str.Remove(0, 1); // Strip the leading '/' from the switch. if (handlers.ContainsKey(param)) { Log.Warning("Consuming command-line switch '" + param + "'."); handlers[param](); } else { ++badSwitchCount; Log.Warning("Ignoring unrecognized command-line switch '" + param + "'."); } } if (badSwitchCount > 0 && ShouldShowMessage(MessageVerbosity.NoError)) { ShowBalloonTip(2000, "Invalid switch", badSwitchCount.ToString() + " invalid switch" + (badSwitchCount > 1 ? "es were" : " was") + " passed to EasyImgur (see log for details). No files were uploaded.", ToolTipIcon.Error, true); return; } // Process actual arguments foreach (string path in e.Args.Where(s => s != null && !s.StartsWith("/"))) { if (!anonymous && !ImgurAPI.HasBeenAuthorized()) { ShowBalloonTip(2000, "Not logged in", "You aren't logged in but you're trying to upload to an account. Authorize EasyImgur and try again.", ToolTipIcon.Error, true); return; } if (Directory.Exists(path)) { string[] fileTypes = new[] { ".jpg", ".jpeg", ".png", ".apng", ".bmp", ".gif", ".tiff", ".tif", ".xcf" }; List <string> files = new List <string>(); foreach (string s in Directory.GetFiles(path)) { bool cont = false; foreach (string filetype in fileTypes) { if (s.EndsWith(filetype, true, null)) { cont = true; break; } } if (!cont) { continue; } files.Add(s); } UploadAlbum(anonymous, files.ToArray(), path.Split('\\').Last()); } else if (File.Exists(path)) { UploadFile(anonymous, new string[] { path }); } } } catch (Exception ex) { Log.Error("Unhandled exception in context menu thread: " + ex.ToString()); ShowBalloonTip(2000, "Error", "An unknown exception occurred during upload. Check the log for further information.", ToolTipIcon.Error, true); } if (exitWhenFinished) { Application.Exit(); } // reset verbosity Verbosity = MessageVerbosity.Normal; }
bool ShouldShowMessage(MessageVerbosity _Verbosity) { return(Verbosity < _Verbosity); }
/// <summary> /// Sends message to the log file. An RFC822 timestamp is pre-pended to the message by default. /// </summary> /// <param name="message">Text to send to log file</param> /// <param name="mv">Verbosity of message.</param> public void Log(string message, MessageVerbosity mv) { if (_verbosity == MessageVerbosity.Undefined || mv == MessageVerbosity.Undefined) return; if ((int)mv > (int)_verbosity) return; _logLock.WaitOne(); _logMessages.Enqueue(DateTime.Now.ToString(_timeFormat) + "> " + message); _logLock.ReleaseMutex(); }
/// <summary> /// Log a message with verbosity specification /// </summary> /// <param name="message"></param> /// <param name="type"></param> public static void Log(string message, MessageVerbosity type = MessageVerbosity.Info) { InnerLog(message, type, System.Reflection.Assembly.GetCallingAssembly() == typeof(Tracer).Assembly); }
void singleInstance_ArgumentsReceived( object sender, ArgumentsReceivedEventArgs e ) { // Using "/exit" anywhere in the command list will cause EasyImgur to exit after uploading; // this will happen regardless of the execution sending the exit command was the execution that // launched the initial instance of EasyImgur. bool exitWhenFinished = false; bool anonymous = false; // mappings of switch names to actions Dictionary<string, Action> handlers = new Dictionary<string, Action>() { { "anonymous", () => anonymous = true }, { "noinfo", () => Verbosity = (MessageVerbosity)Math.Max((int)Verbosity, (int)MessageVerbosity.NoInfo) }, { "q", () => Verbosity = (MessageVerbosity)Math.Max((int)Verbosity, (int)MessageVerbosity.NoInfo) }, { "noerr", () => Verbosity = (MessageVerbosity)Math.Max((int)Verbosity, (int)MessageVerbosity.NoError) }, { "qq", () => Verbosity = (MessageVerbosity)Math.Max((int)Verbosity, (int)MessageVerbosity.NoError) }, { "exit", () => exitWhenFinished = true }, { "portable", () => { } } // ignore }; try { // First scan for switches int badSwitchCount = 0; foreach (String str in e.Args.Where(s => s != null && s.StartsWith("/"))) { String param = str.Remove(0, 1); // Strip the leading '/' from the switch. if (handlers.ContainsKey(param)) { Log.Warning("Consuming command-line switch '" + param + "'."); handlers[param](); } else { ++badSwitchCount; Log.Warning("Ignoring unrecognized command-line switch '" + param + "'."); } } if (badSwitchCount > 0 && ShouldShowMessage(MessageVerbosity.NoError)) { ShowBalloonTip(2000, "Invalid switch", badSwitchCount.ToString() + " invalid switch" + (badSwitchCount > 1 ? "es were" : " was") + " passed to EasyImgur (see log for details). No files were uploaded.", ToolTipIcon.Error, true); return; } // Process actual arguments foreach(string path in e.Args.Where(s => s != null && !s.StartsWith("/"))) { if(!anonymous && !ImgurAPI.HasBeenAuthorized()) { ShowBalloonTip(2000, "Not logged in", "You aren't logged in but you're trying to upload to an account. Authorize EasyImgur and try again.", ToolTipIcon.Error, true); return; } if(Directory.Exists(path)) { string[] fileTypes = new[] { ".jpg", ".jpeg", ".png", ".apng", ".bmp", ".gif", ".tiff", ".tif", ".xcf" }; List<string> files = new List<string>(); foreach (string s in Directory.GetFiles(path)) { bool cont = false; foreach (string filetype in fileTypes) if (s.EndsWith(filetype, true, null)) { cont = true; break; } if (!cont) continue; files.Add(s); } UploadAlbum(anonymous, files.ToArray(), path.Split('\\').Last()); } else if(File.Exists(path)) UploadFile(anonymous, new string[] { path }); } } catch(Exception ex) { Log.Error("Unhandled exception in context menu thread: " + ex.ToString()); ShowBalloonTip(2000, "Error", "An unknown exception occurred during upload. Check the log for further information.", ToolTipIcon.Error, true); } if(exitWhenFinished) Application.Exit(); // reset verbosity Verbosity = MessageVerbosity.Normal; }
bool ShouldShowMessage(MessageVerbosity _Verbosity) { return Verbosity < _Verbosity; }