using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Misc
{
    /// <summary>
    /// 保存调试信息
    /// </summary>
    public class Log
    {
        /// <summary>
        /// 当前级别限制
        /// </summary>
        public static int Level=0;
        
        
        static Log()
        {
            ReadLv();
        }
        static void ReadLv() 
        {
            try
            {
                StreamReader sr = new StreamReader("logparam.txt", Encoding.GetEncoding("GB2312"));
                string s = sr.ReadLine();
                int level = 0;
                if (int.TryParse(s, out level))
                {
                    Level = level;
                }
                sr.Close();
            }
            catch 
            {
            
            }
        }
        static bool SaveLv() 
        {
            try{
                StreamWriter sw = new StreamWriter("logparam.txt", false, Encoding.GetEncoding("GB2312"));
                sw.WriteLine(Level.ToString());
                sw.Flush();
                sw.Close();  
                return true;         
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 设置级别限制
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public static bool SetLv(int level) 
        {
            Level = level;
            return SaveLv();
        }

        /// <summary>
        /// 输出调试信息
        /// </summary>
        /// <param name="logClass"></param>
        /// <param name="level"></param>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void LogMessage(string logClass, int level, string format, params object[] args)
        {	
            if( level > Level )
                return;
            

            //以附加的方式打开只写文件。
            //若文件不存在，则会建立该文件，
            //如果文件存在，写入的数据会被加到文件尾，即文件原先的内容会被保留。
            StreamWriter sw = new StreamWriter("log.txt", true, Encoding.GetEncoding("GB2312"));
            string time_str = DateTime.Now.ToString();
            sw.Write(time_str + ",");
            sw.Write(level.ToString() +",");
            sw.Write( logClass.ToString()+",");
            sw.WriteLine(format, args);
            sw.Flush();
            sw.Close();
        }

        /// <summary>
        /// 输出调试信息
        /// </summary>
        /// <param name="logClass"></param>
        /// <param name="level"></param>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void LogMessage(string logClass, int level, string format)
        {
            if (level > Level)
                return;

            //以附加的方式打开只写文件。
            //若文件不存在，则会建立该文件，
            //如果文件存在，写入的数据会被加到文件尾，即文件原先的内容会被保留。
            StreamWriter sw = new StreamWriter("log.txt", true, Encoding.GetEncoding("GB2312"));
            string time_str = DateTime.Now.ToString();
            sw.Write(time_str + ",");
            sw.Write(level.ToString() + ",");
            sw.Write(logClass.ToString() + ",");
            sw.WriteLine(format);
            sw.Flush();
            sw.Close();
        }
    }
}
