Ejemplo n.º 1
0
        private static void Internal_Log(string message, LogType type)
        {
            // Null case.
            if (message == " ")
            {
                message = "Null";
            }

            switch (type)
            {
            case LogType.Log:
                Debug.Log(message);
                break;

            case LogType.Warning:
                Debug.LogWarning(message);
                break;

            case LogType.Error:
                Debug.LogError(message);
                break;

            case LogType.Assert:
                break;

            case LogType.Exception:
                break;

            default:
                throw new ArgumentOutOfRangeException(type.GetType().FullName, type, null);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 写入日志队列中,等待系统统一处理
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        private void WriteToFile(LogType type, object message, Exception exception = null)
        {
            // 获取枚举的描述信息
            var    objType = type.GetType().GetField(type.ToString()).GetCustomAttributes(false);
            string strType = objType.Length > 0 ? ((EnumDescriptionAttribute)objType[0]).Description: "Log";
            // 得到日志数据
            string log = string.Empty;

            if (exception == null)
            {
                log = string.Format("[{0}]{1}:{2}{3}",
                                    DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), strType,
                                    (message == null ? "null" : message.ToString()), Environment.NewLine);
            }
            else
            {
#if DEBUG
                log = string.Format("[{0}]{1}:{2}|Exception:{3}{4}",
                                    DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), strType,
                                    (message == null ? "null" : message.ToString()), exception.ToString(), Environment.NewLine);
#else
                log = string.Format("[{0}]{1}:{2}|Exception:{3}{4}",
                                    DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), strType,
                                    (message == null ? "null" : message.ToString()), exception.Message, Environment.NewLine);
#endif
            }

            LoggerManager.AddLogger(log);
        }
Ejemplo n.º 3
0
        public static string GetDescriptionText(this LogType source)
        {
            FieldInfo fi = source.GetType().GetField(source.ToString());

            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
                typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
            {
                return(attributes[0].Description);
            }
            else
            {
                return(source.ToString());
            }
        }
Ejemplo n.º 4
0
        /* Checks if config file exists, if not then create it.
         * Then if file is not locked, append text to it.
         * Also if type is fatal, end the process.
         */
        public static void Write(LogType type, string message = "", bool startTime = false)
        {
            // Check if file exists, if not then create it.
            try
            {
                if (!logFile.Exists)
                {
                    logFile.Create();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("FATAL: Could not find nor create a log file: {0}", ex.Message);
                Environment.Exit(1);
            }



            if (!isConfigLocked())
            {
                // Get time
                string time = DateTime.Now.ToString();

                // Get enum desc
                DescriptionAttribute[] attributes = (DescriptionAttribute[])type.GetType().GetField(type.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
                string logtype = attributes.Length > 0 ? attributes[0].Description : string.Empty;


                // Append text
                try
                {
                    using (StreamWriter sw = logFile.AppendText())
                    {
                        if (startTime)
                        {
                            sw.WriteLine("");
                            sw.WriteLine();

                            if (Config.GetString("logToConsole") == "true")
                            {
                                Console.WriteLine("# Program started at " + time);
                            }
                        }
                        else
                        {
                            sw.WriteLine(time + " - " + logtype + " - " + message);

                            if (Config.GetString("logToConsole") == "true")
                            {
                                Console.WriteLine(time + " - " + logtype + " - " + message);
                            }
                        }
                    }
                }
                catch (IOException ex)
                {
                    Console.WriteLine("WARNING: Unable to append text into log file: {0}", ex.Message);
                }
            }

            if (type == LogType.FATAL)
            {
                if ((Config.GetString("debug") == "true"))
                {
                    Console.WriteLine("CRITICAL ERROR - SHOULD EXIT");
                    Console.Read();
                }
                else
                {
                    Environment.Exit(1);
                }
            }
        }
Ejemplo n.º 5
0
 public int getImageIndex()
 {
     return(Array.IndexOf(Enum.GetValues(LogType.GetType()), LogType));
 }