Skip to content

Commit

Permalink
Updated wizard to use ConsoleLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Jan 23, 2024
1 parent 40c4456 commit 59afaec
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 43 deletions.
15 changes: 8 additions & 7 deletions ideexpert/DMVC.Expert.CodeGen.Templates.pas
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ interface
'procedure RunServer(APort: Integer);' + sLineBreak +
'var' + sLineBreak +
' LServer: TIdHTTPWebBrokerBridge;' + sLineBreak +
'begin' + sLineBreak +
' Writeln(''** DMVCFramework Server ** build '' + DMVCFRAMEWORK_VERSION);' + sLineBreak +
'begin' + sLineBreak +
' LServer := TIdHTTPWebBrokerBridge.Create(nil);' + sLineBreak +
' try' + sLineBreak +
' LServer.OnParseAuthentication := TMVCParseAuthentication.OnParseAuthentication;' + sLineBreak +
Expand All @@ -81,8 +80,8 @@ interface
' LServer.MaxConnections := dotEnv.Env(''dmvc.webbroker.max_connections'', 0);' + sLineBreak +
' LServer.ListenQueue := dotEnv.Env(''dmvc.indy.listen_queue'', 500);' + sLineBreak + sLineBreak +
' LServer.Active := True;' + sLineBreak +
' WriteLn(''Listening on port '', APort);' + sLineBreak +
' Write(''CTRL+C to shutdown the server'');' + sLineBreak +
' LogI(''Listening on port '' + APort.ToString);' + sLineBreak +
' LogI(''CTRL+C to shutdown the server'');' + sLineBreak +
' WaitForTerminationSignal; ' + sLineBreak +
' EnterInShutdownState; ' + sLineBreak +
' LServer.Active := False; ' + sLineBreak +
Expand All @@ -98,7 +97,9 @@ interface
' // DMVCFramework Specific Configuration ' + sLineBreak +
' // When MVCSerializeNulls = True empty nullables and nil are serialized as json null.' + sLineBreak +
' // When MVCSerializeNulls = False empty nullables and nil are not serialized at all.' + sLineBreak +
' MVCSerializeNulls := True;' + sLineBreak + sLineBreak +
' MVCSerializeNulls := True;' + sLineBreak +
' UseConsoleLogger := True;' + sLineBreak + sLineBreak +
' LogI(''** DMVCFramework Server ** build '' + DMVCFRAMEWORK_VERSION);' + sLineBreak +
' try' + sLineBreak +
' if WebRequestHandler <> nil then' + sLineBreak +
' WebRequestHandler.WebModuleClass := WebModuleClass;' + sLineBreak +
Expand All @@ -113,7 +114,7 @@ interface
' .UseProfile(''prod'') //if available loads the prod environment (.env.prod)' + sLineBreak +
' .UseLogger(procedure(LogItem: String)' + sLineBreak +
' begin' + sLineBreak +
' LogW(''dotEnv: '' + LogItem);' + sLineBreak +
' LogD(''dotEnv: '' + LogItem);' + sLineBreak +
' end)' + sLineBreak +
' .Build(); //uses the executable folder to look for .env* files' + sLineBreak +
' end);' + sLineBreak +
Expand All @@ -129,7 +130,7 @@ interface
' RunServer(dotEnv.Env(''dmvc.server.port'', %1:d));' + sLineBreak +
' except' + sLineBreak +
' on E: Exception do' + sLineBreak +
' Writeln(E.ClassName, '': '', E.Message);' + sLineBreak +
' LogF(E.ClassName + '': '' + E.Message);' + sLineBreak +
' end;' + sLineBreak +
'end.' + sLineBreak;

Expand Down
4 changes: 2 additions & 2 deletions lib/loggerpro/LoggerPro.ConsoleAppender.pas
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ procedure TLoggerProConsoleAppender.WriteLog(const aLogItem: TLogItem);
TLogType.Info:
lColor := FOREGROUND_BLUE or FOREGROUND_GREEN or FOREGROUND_RED;
TLogType.Warning:
lColor := FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_INTENSITY;
lColor := FOREGROUND_RED or FOREGROUND_GREEN;
TLogType.Error:
lColor := FOREGROUND_RED or FOREGROUND_INTENSITY;
lColor := FOREGROUND_RED;
TLogType.Fatal:
lColor := FOREGROUND_RED or FOREGROUND_BLUE or FOREGROUND_INTENSITY;
end;
Expand Down
3 changes: 2 additions & 1 deletion lib/loggerpro/packages/d120/loggerproRT.dpk
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ contains
LoggerPro.Utils in '..\..\LoggerPro.Utils.pas',
ThreadSafeQueueU in '..\..\ThreadSafeQueueU.pas',
LoggerPro.OutputDebugStringAppender in '..\..\LoggerPro.OutputDebugStringAppender.pas',
LoggerPro.Renderers in '..\..\LoggerPro.Renderers.pas';
LoggerPro.Renderers in '..\..\LoggerPro.Renderers.pas',
LoggerPro.ConsoleAppender in '..\..\LoggerPro.ConsoleAppender.pas';

end.
1 change: 1 addition & 0 deletions lib/loggerpro/packages/d120/loggerproRT.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<DCCReference Include="..\..\ThreadSafeQueueU.pas"/>
<DCCReference Include="..\..\LoggerPro.OutputDebugStringAppender.pas"/>
<DCCReference Include="..\..\LoggerPro.Renderers.pas"/>
<DCCReference Include="..\..\LoggerPro.ConsoleAppender.pas"/>
<BuildConfiguration Include="Base">
<Key>Base</Key>
</BuildConfiguration>
Expand Down
2 changes: 1 addition & 1 deletion samples/articles_crud_server/WebModuleUnit1.pas
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ procedure TWebModule1.WebModuleCreate(Sender: TObject);
{$ENDIF}
FEngine.AddMiddleware(TCORSMiddleware.Create);
FEngine.AddMiddleware(TMVCCompressionMiddleware.Create(256));
FEngine.AddMiddleware(TMVCTraceMiddleware.Create);
// FEngine.AddMiddleware(TMVCTraceMiddleware.Create);
end;

end.
9 changes: 4 additions & 5 deletions samples/articles_crud_server/articles_crud_server.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@ procedure RunServer(APort: Integer);
var
LServer: TIdHTTPWebBrokerBridge;
begin
WriteLn('ARTICLES CRUD Sample. Use articles_crud_vcl_client.dproj to manage data');
Writeln('** DMVCFramework Server ** build ' + DMVCFRAMEWORK_VERSION);
LogI('** DMVCFramework Server ** build ' + DMVCFRAMEWORK_VERSION);
LogW('ARTICLES CRUD Sample. Use articles_crud_vcl_client.dproj to manage data');
LServer := TIdHTTPWebBrokerBridge.Create(nil);
try
LServer.OnParseAuthentication := TMVCParseAuthentication.OnParseAuthentication;
LServer.DefaultPort := APort;
LServer.KeepAlive := True;
LServer.MaxConnections := dotEnv.Env('dmvc.webbroker.max_connections', 0);
LServer.ListenQueue := dotEnv.Env('dmvc.indy.listen_queue', 500);

LServer.Active := True;
WriteLn('Listening on port ', APort);
Write('CTRL+C to shutdown the server');
LogI('Listening on port ' + APort.ToString);
LogI('CTRL+C to shutdown the server');
WaitForTerminationSignal;
EnterInShutdownState;
LServer.Active := False;
Expand Down
132 changes: 131 additions & 1 deletion samples/articles_crud_server/articles_crud_server.dproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{1576AA4D-0623-40AC-97D3-AA4BB4381A0A}</ProjectGuid>
<ProjectVersion>19.5</ProjectVersion>
<ProjectVersion>20.1</ProjectVersion>
<FrameworkType>VCL</FrameworkType>
<MainSource>articles_crud_server.dpr</MainSource>
<Base>True</Base>
Expand Down Expand Up @@ -225,6 +225,16 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidSplashImageDefV21">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidSplashStyles">
<Platform Name="Android">
<RemoteDir>res\values</RemoteDir>
Expand All @@ -245,6 +255,66 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="AndroidSplashStylesV31">
<Platform Name="Android">
<RemoteDir>res\values-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\values-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIcon">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v26</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v26</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconBackground">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconForeground">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconMonochrome">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_AdaptiveIconV33">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v33</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v33</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_Colors">
<Platform Name="Android">
<RemoteDir>res\values</RemoteDir>
Expand All @@ -255,6 +325,16 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_ColorsDark">
<Platform Name="Android">
<RemoteDir>res\values-night-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\values-night-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_DefaultAppIcon">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
Expand Down Expand Up @@ -425,6 +505,56 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedNotificationIcon">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v24</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v24</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplash">
<Platform Name="Android">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplashDark">
<Platform Name="Android">
<RemoteDir>res\drawable-night-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-night-anydpi-v21</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplashV31">
<Platform Name="Android">
<RemoteDir>res\drawable-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="Android_VectorizedSplashV31Dark">
<Platform Name="Android">
<RemoteDir>res\drawable-night-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
<Platform Name="Android64">
<RemoteDir>res\drawable-night-anydpi-v31</RemoteDir>
<Operation>1</Operation>
</Platform>
</DeployClass>
<DeployClass Name="DebugSymbols">
<Platform Name="iOSSimulator">
<Operation>1</Operation>
Expand Down
57 changes: 31 additions & 26 deletions sources/MVCFramework.Logger.pas
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ procedure LogW(AMessage: string); overload;
procedure LogW(AObject: TObject); overload;

procedure LogE(AMessage: string);

procedure LogF(AMessage: string);

procedure Log(LogLevel: TLogLevel; const AMessage: string); overload;

procedure LogException(const E: Exception; const AMessage: String);
Expand All @@ -96,11 +99,14 @@ procedure InitThreadVars;

var
LogLevelLimit: TLogLevel = TLogLevel.levNormal;
UseConsoleLogger: Boolean = True;

implementation

uses
{$IF Defined(CONSOLE)}
{$IF Defined(MSWINDOWS)}
LoggerPro.ConsoleAppender,
{$ELSE}
LoggerPro.SimpleConsoleAppender,
{$ENDIF}
LoggerPro.Renderers,
Expand Down Expand Up @@ -193,6 +199,11 @@ procedure LogE(AMessage: string);
Log.Error(AMessage, LOGGERPRO_TAG);
end;

procedure LogF(AMessage: string);
begin
Log.Fatal(AMessage, LOGGERPRO_TAG);
end;

procedure LogException(const E: Exception; const AMessage: String);
begin
LogE(E.ClassName + ': ' + E.Message + ' - (Custom Message: ' + AMessage + ')');
Expand Down Expand Up @@ -276,7 +287,7 @@ procedure SetDefaultLogger(const aLogWriter: ILogWriter);
else
begin
InitializeDefaultLogger;
Log.Info('Default Logger initialized', LOGGERPRO_TAG);
//Log.Info('Default Logger initialized', LOGGERPRO_TAG);
end;
end;
finally
Expand All @@ -286,32 +297,12 @@ procedure SetDefaultLogger(const aLogWriter: ILogWriter);
end;


{$IF Defined(CONSOLE)}
procedure InitializeDefaultLogger;
var
lLogsFolder: String;
begin
{ This procedure must be called in a synchronized context
(Normally only SetDefaultLogger should be the caller) }
if not Assigned(gDefaultLogger) then
begin
{$IF NOT DEFINED(MOBILE)}
lLogsFolder := AppPath + 'logs';
{$ELSE}
lLogsFolder := TPath.Combine(TPath.GetDocumentsPath, 'logs');
{$ENDIF}
gDefaultLogger := BuildLogWriter([
TLoggerProFileAppender.Create(5, 2000, lLogsFolder),
TLoggerProSimpleConsoleAppender.Create(TLogItemRendererNoTag.Create)
]);
end;
end;
{$ENDIF}

{$IF not Defined(CONSOLE)}
procedure InitializeDefaultLogger;
var
lLogsFolder: String;
lFileAppender, lConsoleAppender: ILogAppender;
lAppenders: TArray<ILogAppender>;
begin
{ This procedure must be called in a synchronized context
(Normally only SetDefaultLogger should be the caller) }
Expand All @@ -322,10 +313,24 @@ procedure InitializeDefaultLogger;
{$ELSE}
lLogsFolder := TPath.Combine(TPath.GetDocumentsPath, 'logs');
{$ENDIF}
gDefaultLogger := BuildLogWriter([TLoggerProFileAppender.Create(5, 2000, lLogsFolder)]);
lFileAppender := TLoggerProFileAppender.Create(5, 2000, lLogsFolder);
if IsConsole and UseConsoleLogger then
begin
{$IF Defined(MSWINDOWS)}
lConsoleAppender := TLoggerProConsoleAppender.Create(TLogItemRendererNoTag.Create);
{$ELSE}
lConsoleAppender := TLoggerProSimpleConsoleAppender.Create(TLogItemRendererNoTag.Create);
{$ENDIF}
lAppenders := [lFileAppender, lConsoleAppender];
end
else
begin
lAppenders := [lFileAppender];
end;
gDefaultLogger := BuildLogWriter(lAppenders);
end;
end;
{$ENDIF}


procedure ReleaseGlobalLogger;
begin
Expand Down

0 comments on commit 59afaec

Please sign in to comment.