-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.ncl
90 lines (75 loc) · 2.2 KB
/
logging.ncl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
; Functions for logging
; Log levels
undef("LOG_LEVELS")
LOG_LEVELS = (/"ALL","DEBUG","INFO","WARN","ERROR","FATAL","ALL"/)
; Default options, can be overwritten before or after import
if (.not.isdefined("LOG_PATH")) then LOG_PATH = "./output.log" end if
if (.not.isdefined("LOG_LEVEL")) then LOG_LEVEL = "DEBUG" end if
if (.not.isdefined("LOG_DATE_FORMAT")) then LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S" end if
; Script name is taken from the command line
if (.not.isdefined("LOG_SCRIPT_NAME")) then
LOG_SCRIPT_NAME = get_script_name()
if (ismissing(LOG_SCRIPT_NAME)) then
LOG_SCRIPT_NAME = "Unknown"
end if
end if
undef("_log_date")
function _log_date()
; Returns the log date
; Returns:
; timestamp (string) : The timestamp matching the format requested
begin
return systemfunc("date +'" + LOG_DATE_FORMAT + "'")
end
undef("_log")
procedure _log(level:string, message)
; Writes to the log file
; Args:
; level (string) : One of LOG_LEVELS
; message (string) : Message that is (or can be cast to) a string
begin
; Only print messages greater than or equal to current level
if (ind(LOG_LEVELS.eq.level).ge.ind(LOG_LEVELS.eq.LOG_LEVEL)) then
system("echo " + _log_date() + " - " + level + " - " + LOG_SCRIPT_NAME + " - " + tostring(message) + " >> " + LOG_PATH)
end if
end
undef("log_debug")
procedure log_debug(message)
; Log a debug message
; Args:
; message (string) : Message that is (or can be cast to) a string
begin
_log("DEBUG", message)
end
undef("log_info")
procedure log_info(message)
; Log an info message
; Args:
; message (string) : Message that is (or can be cast to) a string
begin
_log("INFO", message)
end
undef("log_warn")
procedure log_warn(message)
; Log a warn message
; Args:
; message (string) : Message that is (or can be cast to) a string
begin
_log("WARN", message)
end
undef("log_error")
procedure log_error(message)
; Log an error message
; Args:
; message (string) : Message that is (or can be cast to) a string
begin
_log("ERROR", message)
end
undef("log_fatal")
procedure log_fatal(message)
; Log a fatal message
; Args:
; message (string) : Message that is (or can be cast to) a string
begin
_log("FATAL", message)
end