static void MainLoop() { while (true) { Log log; while (PendingLogs.Count > 0) { lock (lockObject) { log = PendingLogs.Dequeue(); if (!Starter.GuiMode) { if (log.type == LogType.Debug) { #if DEBUG Console.WriteLine("[{0}] [{1}] {2}", log.type.ToString().PadRight(7), log.time.ToString("HH:mm:ss"), log.data); break; #endif } else { Console.WriteLine("[{0}] [{1}] {2}", log.type.ToString().PadRight(7), log.time.ToString("HH:mm:ss"), log.data); } } else { throw new NotImplementedException(); } } } Thread.Sleep(10); } }
/// <summary> /// A thread safe logger /// </summary> /// <param name="m"></param> public static void Log(LogCommand m) { if (!IsMainThread) { lock (syncRoot) { PendingLogs.Add(m); } } else { Write(m); } }
private static object UploadProcess(object userSpecific) { Notice("CloudLog UploadProcess Started"); while (true) { CrestronEnvironment.AllowOtherAppsToRun(); if (_linkDown && !_programEnding) { Thread.Sleep(60000); } else if (!_linkDown) { try { var entries = PendingLogs.Take(50); try { #if DEBUG //CrestronConsole.PrintLine("Updating autodiscovery query..."); #endif var query = EthernetAutodiscovery.Query(); #if DEBUG //CrestronConsole.PrintLine("Autodiscovery {0}", query); #endif if (query == EthernetAutodiscovery.eAutoDiscoveryErrors.AutoDiscoveryOperationSuccess) { var adresults = EthernetAutodiscovery.DiscoveredElementsList; #if DEBUG //CrestronConsole.PrintLine("Found {0} devivces:", adresults.Count); #endif foreach (var adresult in adresults) { //CrestronConsole.PrintLine("{0}, {1}, {2}", adresult.IPAddress, //adresult.HostName, adresult.DeviceIdString); } } #if DEBUG //CrestronConsole.PrintLine("CloudLog uploading to cloud..."); #endif var contents = Post("submit_logs", entries); var data = JObject.Parse(contents); #if DEBUG //CrestronConsole.PrintLine("CloudLog client response: {0}", data["status"].Value<string>()); #endif var results = data["results"]; foreach (var id in results.Select(result => result["id"].Value <string>())) { InternalDictionary[id].CloudId = id; } } catch (Exception e) { Error("Error with CloudLog upload process, {0}", e.Message); if (_programEnding) { return(null); } CrestronConsole.PrintLine("CloudLog could not upload to cloud service. {0} items pending", PendingLogs.Count); Thread.Sleep(60000); } } catch (Exception e) { ErrorLog.Error("Error in CloudLog.PostToCloudProcess, {1}", e.Message); } } if (_programEnding) { return(null); } if (PendingLogs.Count == 0) { UploadEvent.Wait(60000); } } }
/// <summary> /// Logs a regular message /// </summary> public static void Log(string message, LogType type) { Log log = new Log(message, type, DateTime.Now); PendingLogs.Enqueue(log); }