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

namespace STMes
{
    public interface ILogService
    {
        void WriteLog(string message);
    }
    public class CoreWriteLogFile : ILogService
    {
        private static object lockObj = new object();
        private string _postfix = "log";
        private bool _isLoging = false;

        public CoreWriteLogFile()
        {
            _postfix = "log";
        }
        public CoreWriteLogFile(string postfix)
        {
            if (postfix.Trim() == "")
            {
                _postfix = "log";
            }
            else
            {
                _postfix = postfix.Trim();
            }
        }

        public void StartLogging(bool startIt)
        {
            _isLoging = startIt;
        }

        #region ILogService ³ΙΤ±

        public void WriteLog(string message)
        {
            WriteLog(message, LogInfoLevel.Message, "", "");
        }
        public void WriteLog(string message, LogInfoLevel level)
        {
            WriteLog(message, level, "", "");
        }

        public void WriteLog(string message, string UserID, string codeInfo)
        {
            WriteLog(message, LogInfoLevel.Message, UserID, codeInfo);
        }
        public void WriteLog(string message, LogInfoLevel level, string UserID, string codeInfo)
        {
            if (!_isLoging) return;
            lock (lockObj)
            {
                try
                {
                    string logFile = System.Environment.CurrentDirectory + "/log/" + "DBProxy_" + DateTime.Now.ToString("yyyyMMdd") + "_" + _postfix + ".log";
                    FileInfo fi = new FileInfo(logFile);
                    StreamWriter sw = null;
                    try
                    {
                        if (fi.Exists)
                            sw = fi.AppendText();
                        else
                            sw = fi.CreateText();

                        string str = "";
                        if (level == LogInfoLevel.Error)
                            str = ">>>  Error  <<<";
                        else
                            str = ">>>  Message  <<<";

                        string title = ">>>  " + DateTime.Now + "  <<<";
                        title += str;

                        if (UserID != "")
                        {
                            title += ">>>  " + UserID + "  <<<";
                        }
                        if (codeInfo != "")
                        {
                            title += ">>>  " + codeInfo + "  <<<";
                        }

                        sw.WriteLine(title);
                        sw.WriteLine("{0}", message);
                        sw.WriteLine();
                        sw.Flush();
                    }
                    finally
                    {
                        if (sw != null)
                            sw.Close();
                    }
                }
                catch
                {
                    // do nothing.
                }
            }
        }

        #endregion
    }

    public enum LogInfoLevel
    {
        Error = 1,
        Message = 2
    }


}
