diff --git a/SMBServer/LogWriter.cs b/SMBServer/LogWriter.cs new file mode 100644 index 0000000..047790d --- /dev/null +++ b/SMBServer/LogWriter.cs @@ -0,0 +1,111 @@ +/* Copyright (C) 2017 Tal Aloni . All rights reserved. + * + * You can redistribute this program and/or modify it under the terms of + * the GNU Lesser Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + */ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.IO; +using Utilities; + +namespace SMBServer +{ + public class LogWriter + { + private string m_logsDirectoryPath; + private object m_syncLock = new object(); + private FileStream m_logFile; + private DateTime? m_logFileDate; + + public LogWriter() + { + Assembly assembly = Assembly.GetEntryAssembly(); + if (assembly == null) + { + assembly = Assembly.GetExecutingAssembly(); + } + string assemblyDirectory = Path.GetDirectoryName(assembly.Location); + if (!assemblyDirectory.EndsWith(@"\")) + { + assemblyDirectory += @"\"; + } + m_logsDirectoryPath = assemblyDirectory + @"Logs\"; + } + + public LogWriter(string logsDirectoryPath) + { + m_logsDirectoryPath = logsDirectoryPath; + } + + /// + /// + private void OpenLogFile() + { + if (m_logFileDate.HasValue && m_logFileDate.Value != DateTime.Today && m_logFile != null) + { + m_logFile.Close(); + m_logFile = null; + m_logFileDate = null; + } + + if (m_logFileDate == null) + { + m_logFileDate = DateTime.Today; + string logFilePath = String.Format("{0}{1}-{2}-{3}.log", m_logsDirectoryPath, DateTime.Now.Year, DateTime.Now.Month.ToString("00"), DateTime.Now.Day.ToString("00")); + try + { + if (!Directory.Exists(m_logsDirectoryPath)) + { + Directory.CreateDirectory(m_logsDirectoryPath); + } + m_logFile = new FileStream(logFilePath, FileMode.Append, FileAccess.Write, FileShare.Read, 0x1000, FileOptions.WriteThrough); + } + catch + { + } + } + } + + public void CloseLogFile() + { + if (m_logFile != null) + { + lock (m_syncLock) + { + m_logFile.Close(); + m_logFile = null; + } + } + } + + public void WriteLine(string value, params object[] args) + { + WriteLine(String.Format(value, args)); + } + + public void WriteLine(string value) + { + lock (m_syncLock) + { + OpenLogFile(); + if (m_logFile != null) + { + StreamWriter writer = new StreamWriter(m_logFile); + writer.WriteLine(value); + writer.Flush(); + } + } + } + + public void OnLogEntry(object sender, LogEntry entry) + { + if (entry.Severity != Severity.Trace) + { + string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss "); + WriteLine("{0} {1} [{2}] {3}", entry.Severity.ToString().PadRight(12), timestamp, entry.Source, entry.Message); + } + } + } +} diff --git a/SMBServer/SMBServer.csproj b/SMBServer/SMBServer.csproj index d3d9f66..cd78b4d 100644 --- a/SMBServer/SMBServer.csproj +++ b/SMBServer/SMBServer.csproj @@ -39,6 +39,7 @@ + Form diff --git a/SMBServer/ServerUI.cs b/SMBServer/ServerUI.cs index 3733f06..5c8e2e2 100644 --- a/SMBServer/ServerUI.cs +++ b/SMBServer/ServerUI.cs @@ -30,6 +30,7 @@ namespace SMBServer public const string SettingsFileName = "Settings.xml"; private SMBLibrary.Server.SMBServer m_server; private SMBLibrary.Server.NameServer m_nameServer; + private LogWriter m_logWriter; public ServerUI() { @@ -97,7 +98,8 @@ namespace SMBServer GSSProvider securityProvider = new GSSProvider(authenticationMechanism); m_server = new SMBLibrary.Server.SMBServer(shares, securityProvider); - m_server.OnLogEntry += new EventHandler(Server_OnLogEntry); + m_logWriter = new LogWriter(); + m_server.OnLogEntry += new EventHandler(m_logWriter.OnLogEntry); try { @@ -210,6 +212,7 @@ namespace SMBServer private void btnStop_Click(object sender, EventArgs e) { m_server.Stop(); + m_logWriter.CloseLogFile(); btnStart.Enabled = true; btnStop.Enabled = false; comboIPAddress.Enabled = true; @@ -225,13 +228,6 @@ namespace SMBServer } } - private void Server_OnLogEntry(object sender, LogEntry entry) - { - string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss "); - string message = String.Format("{0} {1} {2}", entry.Severity.ToString().PadRight(12), timestamp, entry.Message); - System.Diagnostics.Debug.Print(message); - } - private void chkSMB1_CheckedChanged(object sender, EventArgs e) { if (!chkSMB1.Checked)