summaryrefslogtreecommitdiff
path: root/lib/gitano/log.lua
blob: f243b8796591076d4c5be19ee59d5b4c3a13c8d2 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
-- gitano.log
--
-- Statement logging depending on log level etc.
--
-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>

local luxio = require "luxio"
local sio = require "luxio.simple"
local os = require "os"

local concat = table.concat

local prefix = "[gitano] "
local transactionid = nil

local stream = sio.stderr

local ERRS = 0
local WARN = 1
local CHAT = 2
local INFO = 3
local DEBUG = 4
local DEEPDEBUG = 5

local level = ERRS

local function syslog_write(priority, ...)
   local strs = {...}

   for i = 1, #strs do
      strs[i] = tostring(strs[i]) or "?"
   end

   luxio.syslog(priority, transactionid .. ": " .. concat(strs, " ") .. "\n")
end

local function syslog_open()
   local ident = "gitano"
   transactionid = luxio.getenv("GITANO_TRANSACTION_ID")

   if not transactionid then
      transactionid = tostring(luxio.getpid()) .. "." .. os.date("%H%M%S")
   end

   luxio.openlog(ident, 0, luxio.LOG_DAEMON)

   return transactionid
end

local function syslog_close()
   luxio.closelog()
end

local function syslog_error(...)
   syslog_write(luxio.LOG_ERR, ...)
end

local function syslog_warning(...)
   syslog_write(luxio.LOG_WARNING, ...)
end

local function syslog_notice(...)
   syslog_write(luxio.LOG_NOTICE, ...)
end

local function syslog_info(...)
   syslog_write(luxio.LOG_INFO, ...)
end

local function syslog_debug(...)
   syslog_write(luxio.LOG_DEBUG, ...)
end

local function set_prefix(new_prefix)
   if not new_prefix then
      prefix = ""
   else
      if new_prefix == "true" then
	 new_prefix = "Gitano"
      end
      prefix = "[" .. tostring(new_prefix) .. "] "
   end
end

local function AT(LVL, ...)
   if level >= LVL then
      local strs = {...}
      for i = 1, #strs do
	 strs[i] = tostring(strs[i]) or "?"
      end
      stream:write(prefix .. concat(strs, " ") .. "\n")
   end
end

local function state(...)
   return AT(-1, ...)
end

local function stdout(...)
   local savedstream, savedprefix = stream, prefix
   stream, prefix = sio.stdout, ""
   state(...)
   stream, prefix = savedstream, savedprefix
end

local function fatal(...)
   syslog_write(luxio.LOG_EMERG, ...)
   AT(ERRS, "FATAL:", ...)
   stream:close()
   luxio._exit(1)
end

local function critical(...)
   syslog_write(luxio.LOG_CRIT, ...)
   return AT(ERRS, "CRIT:", ...)
end

local function error(...)
   return AT(ERRS, "ERROR:", ...)
end

local function warning(...)
   return AT(WARN, "WARNING:", ...)
end
local warn = warning

local function chat(...)
   return AT(CHAT, ...)
end

local function info(...)
   return AT(INFO, "INFO:", ...)
end

local function debug(...)
   return AT(DEBUG, "DEBUG:", ...)
end

local function deepdebug(...)
   return AT(DEEPDEBUG, "DEEPDEBUG:", ...)
end

local function set_level(l)
   if type(l) ~= "number" or
      l < ERRS or l > DEEPDEBUG then
      level = WARN
      warn("Attempted to set level to", tostring(l), "- defaulted to warnings")
   end
   level = l
end

local function bump_level(l)
   if type(l) ~= "number" or
      l < ERRS or l > DEEPDEBUG then
      warn("Attempted to bump level to", tostring(l), "- left alone")
   end
   if level < l then level = l end
end

local function cap_level(l)
   if type(l) ~= "number" or
      l < ERRS or l > DEEPDEBUG then
      warn("Attempted to cap level to", tostring(l), "- left alone")
   end
   if level > l then level = l end
end

local function get_level()
   return level
end

-- Check for default log level based on environment
do
   local loglevel = luxio.getenv("LC_GITANO_LOG_LEVEL") or 
      luxio.getenv("GITANO_LOG_LEVEL")
   if loglevel then
      if tonumber(loglevel) then
	 set_level(tonumber(loglevel))
      else
	 loglevel = loglevel:lower()
	 if (loglevel == "warn" or loglevel == "warning" or
	     loglevel == "warnings") then
	    level = WARN
	 elseif loglevel == "chat" then
	    level = CHAT
	 elseif loglevel == "info" then
	    level = INFO
	 elseif loglevel == "debug" then
	    level = DEBUG
	 elseif (loglevel == "deepdebug" or loglevel == "ddebug" or 
		 loglevel == "all") then
	    level = DEEPDEBUG
	 end
      end
      if level == nil then
	 level = WARN
      end
   end

   local do_debug = luxio.getenv("LC_GITANO_DEBUG") or 
      luxio.getenv("GITANO_DEBUG")
   if do_debug and do_debug ~= "" then
      level = DEBUG
   end
end

return {
   level = {
      ALL = ERRS,
      ERRS = ERRS,
      WARN = WARN,
      CHAT = CHAT,
      INFO = INFO,
      DEBUG = DEBUG,
      DDEBUG = DEEPDEBUG,
      DEEPDEBUG = DEEPDEBUG,
   },
   set_level = set_level,
   get_level = get_level,
   bump_level = bump_level,
   cap_level = cap_level,
   state = state,
   crit = critical,
   critical = critical,
   err = error,
   error = error,
   warn = warning,
   warning = warning,
   chat = chat,
   info = info,
   debug = debug,
   ddebug = deepdebug,
   deepdebug = deepdebug,
   fatal = fatal,
   stdout = stdout,
   set_prefix = set_prefix,
   syslog = {
      open = syslog_open,
      err = syslog_error,
      error = syslog_error,
      warn = syslog_warning,
      warning = syslog_warning,
      notice = syslog_notice,
      info = syslog_info,
      debug = syslog_debug,
      close = syslog_close,
   }
}