@@ -27,7 +27,7 @@ public class LogFormatter
2727 /// <summary>
2828 /// Indicates if more debugging and performance information should be logged.
2929 /// </summary>
30- public bool DebugLoggingEnabled
30+ public bool DebugLoggingEnabled
3131 {
3232 get { return debugLoggingEnabled ; }
3333 set
@@ -38,20 +38,26 @@ public bool DebugLoggingEnabled
3838 }
3939 }
4040
41- public bool debugLoggingEnabled = false ;
41+ private bool debugLoggingEnabled = false ;
4242
4343 private readonly ILog log ;
4444
4545 private readonly string appId ;
4646
47+ private readonly string remoteStorageRootPath ;
48+
49+ private const int sourcePathWidth = 60 ;
50+ private const int remoteStorageIdWidth = 20 ;
51+
4752 /// <summary>
4853 /// Creates instance of this class.
4954 /// </summary>
5055 /// <param name="log">Log4net logger.</param>
51- public LogFormatter ( ILog log , string appId )
56+ public LogFormatter ( ILog log , string appId , string remoteStorageRootPath )
5257 {
5358 this . log = log ;
5459 this . appId = appId ;
60+ this . remoteStorageRootPath = remoteStorageRootPath ;
5561 LogFilePath = ConfigureLogger ( ) ;
5662 }
5763
@@ -84,19 +90,45 @@ public void PrintEnvironmentDescription()
8490 log . Info ( $ "\n { "AppID:" , - 15 } { appId } ") ;
8591 log . Info ( $ "\n { "Engine version:" , - 15 } { typeof ( IEngine ) . Assembly . GetName ( ) . Version } ") ;
8692 log . Info ( $ "\n { "OS version:" , - 15 } { RuntimeInformation . OSDescription } ") ;
87- log . Info ( $ "\n { "Env version:" , - 15 } { RuntimeInformation . FrameworkDescription } { IntPtr . Size * 8 } bit.") ;
93+ log . Info ( $ "\n { ".NET version:" , - 15 } { RuntimeInformation . FrameworkDescription } { IntPtr . Size * 8 } bit.") ;
8894 //log.Info($"\n{"Is UWP:",-15} {PackageRegistrar.IsRunningAsUwp()}");
89- log . Info ( $ "\n { "Admin mode:" , - 15 } { new System . Security . Principal . WindowsPrincipal ( System . Security . Principal . WindowsIdentity . GetCurrent ( ) ) . IsInRole ( System . Security . Principal . WindowsBuiltInRole . Administrator ) } ") ;
95+ log . Info ( $ "\n { "Admin mode:" , - 15 } { new System . Security . Principal . WindowsPrincipal ( System . Security . Principal . WindowsIdentity . GetCurrent ( ) ) . IsInRole ( System . Security . Principal . WindowsBuiltInRole . Administrator ) } ") ;
96+ }
97+
98+ /// <summary>
99+ /// Print Engine config, settings, console commands, logging headers.
100+ /// </summary>
101+ /// <param name="engine">Engine instance.</param>
102+ /// <param name="remoteStorageRootPath">Remote storage root path.</param>
103+ public async Task PrintEngineStartInfoAsync ( EngineWindows engine )
104+ {
105+ await PrintEngineEnvironmentDescriptionAsync ( engine ) ;
106+
107+ // Log console commands.
108+ PrintHelp ( ) ;
109+
110+ // Log logging columns headers.
111+ PrintHeader ( ) ;
112+ }
113+
114+ public async Task PrintEngineEnvironmentDescriptionAsync ( EngineWindows engine )
115+ {
116+ log . Info ( $ "\n { "FS root:" , - 15 } { engine . Path } ") ;
117+ log . Info ( $ "\n { "RS root:" , - 15 } { remoteStorageRootPath } ") ;
118+ log . Info ( $ "\n { "AutoLock:" , - 15 } { engine . AutoLock } ") ;
119+
120+ // Log indexing state. Sync root must be indexed.
121+ await PrintIndexingStateAsync ( engine . Path ) ;
90122 }
91123
92124 /// <summary>
93125 /// Prints indexing state.
94126 /// </summary>
95127 /// <param name="path">File system path.</param>
96- public async Task PrintIndexingStateAsync ( string path )
128+ private async Task PrintIndexingStateAsync ( string path )
97129 {
98130 StorageFolder userFileSystemRootFolder = await StorageFolder . GetFolderFromPathAsync ( path ) ;
99- log . Info ( $ "\n Indexed state: { ( await userFileSystemRootFolder . GetIndexedStateAsync ( ) ) } ") ;
131+ log . Info ( $ "\n { "Indexed state:" , - 15 } { await userFileSystemRootFolder . GetIndexedStateAsync ( ) } ") ;
100132 }
101133
102134 /// <summary>
@@ -108,7 +140,7 @@ public void PrintHelp()
108140 log . Info ( "\n Press Spacebar to exit without unregistering (simulate reboot)." ) ;
109141 log . Info ( "\n Press 'p' to unregister sparse package." ) ;
110142 log . Info ( "\n Press 'e' to start/stop the Engine and all sync services." ) ;
111- log . Info ( "\n Press 's' to start/stop full synchronization service." ) ;
143+ log . Info ( "\n Press 's' to start/stop synchronization service." ) ;
112144 log . Info ( "\n Press 'm' to start/stop remote storage monitor." ) ;
113145 log . Info ( "\n Press 'd' to enable/disable debug and performance logging." ) ;
114146 log . Info ( $ "\n Press 'l' to open log file. ({ LogFilePath } )") ;
@@ -118,17 +150,17 @@ public void PrintHelp()
118150
119151 public void LogError ( IEngine sender , EngineErrorEventArgs e )
120152 {
121- WriteLog ( e , log4net . Core . Level . Error ) ;
153+ WriteLog ( sender , e , log4net . Core . Level . Error ) ;
122154 }
123155
124156 public void LogMessage ( IEngine sender , EngineMessageEventArgs e )
125157 {
126- WriteLog ( e , log4net . Core . Level . Info ) ;
158+ WriteLog ( sender , e , log4net . Core . Level . Info ) ;
127159 }
128160
129161 public void LogDebug ( IEngine sender , EngineMessageEventArgs e )
130162 {
131- WriteLog ( e , log4net . Core . Level . Debug ) ;
163+ WriteLog ( sender , e , log4net . Core . Level . Debug ) ;
132164 }
133165
134166 /// <summary>
@@ -137,23 +169,36 @@ public void LogDebug(IEngine sender, EngineMessageEventArgs e)
137169 /// <param name="log">log4net</param>
138170 /// <param name="e">Message or error description.</param>
139171 /// <param name="level">Log level.</param>
140- private void WriteLog ( EngineMessageEventArgs e , log4net . Core . Level level )
172+ private void WriteLog ( IEngine sender , EngineMessageEventArgs e , log4net . Core . Level level )
141173 {
142174 string att = FsPath . Exists ( e . SourcePath ) ? FsPath . GetAttString ( e . SourcePath ) : null ;
143175 string process = null ;
144176 byte ? priorityHint = null ;
145- ulong ? clientFileId = null ;
177+ string fileId = null ;
146178 string size = null ;
147179
148180 if ( e . OperationContext != null )
149181 {
150182 process = System . IO . Path . GetFileName ( e . OperationContext . ProcessInfo ? . ImagePath ) ;
151183 priorityHint = e . OperationContext . PriorityHint ;
152- clientFileId = ( e . OperationContext as IWindowsOperationContext ) . FileId ;
184+ fileId = ( e . OperationContext as IWindowsOperationContext ) . FileId . ToString ( ) ;
153185 size = FsPath . FormatBytes ( ( e . OperationContext as IWindowsOperationContext ) . FileSize ) ;
154186 }
155187
156- string message = Format ( DateTimeOffset . Now . ToString ( "hh:mm:ss.fff" ) , process , priorityHint ? . ToString ( ) , e . ComponentName , e . Message , e . SourcePath , att , e . TargetPath ) ;
188+ string sourcePath = e . SourcePath ? . FitString ( sourcePathWidth , 6 ) ;
189+ string targetPath = e . TargetPath ? . FitString ( sourcePathWidth , 6 ) ;
190+
191+ // Trim sync root and remote storage root to reduce ammount of logging and improve logs readability
192+ //if (sender != null)
193+ //{
194+ //sourcePath = sourcePath?.Replace((sender as EngineWindows).Path, "<FS root>");
195+ //sourcePath = sourcePath?.Replace(remoteStorageRootPath, "<RS root>");
196+
197+ //targetPath = targetPath?.Replace((sender as EngineWindows).Path, "<FS root>");
198+ //targetPath = targetPath?.Replace(remoteStorageRootPath, "<RS root>");
199+ //}
200+
201+ string message = Format ( DateTimeOffset . Now . ToString ( "hh:mm:ss.fff" ) , process , priorityHint ? . ToString ( ) , fileId , "" , e . ComponentName , e . Message , sourcePath , att , targetPath ) ;
157202
158203 if ( level == log4net . Core . Level . Error )
159204 {
@@ -172,18 +217,36 @@ private void WriteLog(EngineMessageEventArgs e, log4net.Core.Level level)
172217
173218 }
174219
175- private static string Format ( string date , string process , string priorityHint , string componentName , string message , string sourcePath , string attributes , string targetPath )
220+ private static string Format ( string date , string process , string priorityHint , string fileId , string remoteStorageId , string componentName , string message , string sourcePath , string attributes , string targetPath )
176221 {
177- return $ "{ Environment . NewLine } |{ date , - 12 } | { process , - 25 } | { priorityHint , - 5 } | { componentName , - 26 } | { message , - 45 } | { sourcePath , - 80 } | { attributes , - 22 } | { targetPath } ";
222+ // {fileId,-18} | {remoteStorageId,-remoteStorageIdWidth}
223+ return $ "{ Environment . NewLine } |{ date , - 12 } | { process , - 25 } | { priorityHint , - 5 } | { componentName , - 26 } | { message , - 45 } | { sourcePath , - sourcePathWidth } | { attributes , 23 } | { targetPath } ";
178224 }
179225
180226 /// <summary>
181227 /// Prints logging data headers.
182228 /// </summary>
183- public void PrintHeader ( )
229+ private void PrintHeader ( )
230+ {
231+ log . Info ( Format ( "Time" , "Process Name" , "Prty" , "FS ID" , "RS ID" , "Component" , "Message" , "Source Path" , "Attributes" , "Target Path" ) ) ;
232+ log . Info ( Format ( "----" , "------------" , "----" , "_____" , "_____" , "---------" , "-------" , "-----------" , "----------" , "-----------" ) ) ;
233+ }
234+ }
235+
236+ static class StringExtensions
237+ {
238+ private const string replacement = "..." ;
239+ internal static string FitString ( this string str , int maxLength , int startReplace )
184240 {
185- log . Info ( Format ( "Time" , "Process Name" , "Prty" , "Component" , "Operation" , "Source Path" , "Attributes" , "Target Path" ) ) ;
186- log . Info ( Format ( "----" , "------------" , "----" , "---------" , "---------" , "-----------" , "----------" , "-----------" ) ) ;
241+ int length = str . Length ;
242+ if ( length > maxLength )
243+ {
244+ string start = str . Substring ( 0 , startReplace ) ;
245+ string end = str . Substring ( length - ( maxLength - ( startReplace + replacement . Length ) ) ) ;
246+ return $ "{ start } { replacement } { end } ";
247+ }
248+ return str ;
187249 }
188250 }
251+
189252}
0 commit comments