💾 Archived View for gemini.rmf-dev.com › repo › Vaati › rxproxy › files › cbfd2f60fe6c424b5732c7b472… captured on 2023-09-08 at 16:20:40. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-03-20)

🚧 View Differences

-=-=-=-=-=-=-

Go Back

0 /* See LICENSE file for copyright and license details. */

1 module logger;

2 import std.stdio;

3 import std.datetime.date : DateTime;

4 import std.datetime.systime : SysTime, Clock;

5 import std.format;

6 import std.exception;

7

8 /// Write to the log

9 class Logger {

10 private:

11 static string getTime() {

12 SysTime dt = Clock.currTime();

13 return format("[%d-%02d-%02d %02d:%02d:%02d]",dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second);

14 }

15 static bool displayTime = true;

16 static bool writeLog = false;

17 static bool printLog = true;

18 static File fileLog;

19 static string pathLog;

20 public:

21 static void setParameters(bool displayTime, bool writeLog, bool printLog, string pathLog) {

22 this.displayTime=displayTime;

23 this.writeLog=writeLog;

24 if(this.writeLog) {

25 try {

26 fileLog = File(pathLog, "a");

27 } catch(ErrnoException e) {

28 this.writeLog = false;

29 error(e.msg);

30 }

31 }

32 this.printLog=printLog;

33 }

34 /// Print general information to the server logs

35 static void info(string message) {

36 if(printLog) {

37 writefln((displayTime?getTime():"")~"[INFO] "~message);

38 stdout.flush();

39 }

40 if(writeLog) {

41 fileLog.writeln(fileLog, (displayTime?getTime():"")~"[INFO] "~message);

42 //fileLog.flush();

43 }

44 }

45 /// Print warnings to the logs

46 static void warning(string message) {

47 if(printLog) {

48 writefln((displayTime?getTime():"")~"[WARNING] "~message);

49 stdout.flush();

50 }

51 if(writeLog) {

52 fileLog.writeln((displayTime?getTime():"")~"[WARNING] "~message);

53 //fileLog.flush();

54 }

55 }

56 /// Print errors to the logs

57 static void error(string message) {

58 if(printLog) {

59 writefln((displayTime?getTime():"")~"[ERROR] "~message);

60 stdout.flush();

61 }

62 if(writeLog) {

63 fileLog.writeln((displayTime?getTime():"")~"[ERROR] "~message);

64 //fileLog.flush();

65 }

66 }

67 }

68