-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlogging.m
More file actions
286 lines (240 loc) · 8.49 KB
/
Copy pathlogging.m
File metadata and controls
286 lines (240 loc) · 8.49 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
classdef logging < handle
%LOGGING Simple logging framework.
%
% Author:
% Dominique Orban <dominique.orban@gmail.com>
% Heavily modified version of 'log4m': http://goo.gl/qDUcvZ
%
properties (Constant)
ALL = int8(0);
TRACE = int8(1);
DEBUG = int8(2);
INFO = int8(3);
WARNING = int8(4);
ERROR = int8(5);
CRITICAL = int8(6);
OFF = int8(7);
colors_terminal = containers.Map(...
{'normal', 'red', 'green', 'yellow', 'blue', 'brightred'}, ...
{'%s', '\033[31m%s\033[0m', '\033[32m%s\033[0m', '\033[33m%s\033[0m', ...
'\033[34m%s\033[0m', '\033[1;31m%s\033[0m'});
level_colors = containers.Map(...
{logging.logging.INFO, logging.logging.ERROR, logging.logging.TRACE, ...
logging.logging.WARNING, logging.logging.DEBUG, logging.logging.CRITICAL}, ...
{'normal', 'red', 'green', 'yellow', 'blue', 'brightred'});
levels = containers.Map(...
{logging.logging.ALL, logging.logging.TRACE, logging.logging.DEBUG, ...
logging.logging.INFO, logging.logging.WARNING, logging.logging.ERROR, ...
logging.logging.CRITICAL, logging.logging.OFF}, ...
{'ALL', 'TRACE', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL', 'OFF'});
end
properties (SetAccess=immutable)
level_numbers;
level_range;
end
properties (SetAccess=protected)
name;
fullpath = 'logging.log'; % Default log file
logfmt = '%-s %-23s %-8s %s\n';
logfid = -1;
logcolors = logging.logging.colors_terminal;
using_terminal;
end
properties (Hidden,SetAccess=protected)
datefmt_ = 'yyyy-mm-dd HH:MM:SS,FFF';
logLevel_ = logging.logging.INFO;
commandWindowLevel_ = logging.logging.INFO;
bufferedMessages_ = cell(1);
bufferCursor_ = 1;
end
properties (Dependent)
datefmt;
logLevel;
commandWindowLevel;
bufferingSize;
end
methods(Static)
function [name, line] = getCallerInfo(self)
if nargin > 0 && self.ignoreLogging()
name = [];
line = [];
return
end
[ST, ~] = dbstack();
offset = min(size(ST, 1), 3);
name = ST(offset).name;
line = ST(offset).line;
end
end
methods
function setFilename(self, logPath)
[self.logfid, message] = fopen(logPath, 'a');
if self.logfid < 0
warning(['Problem with supplied logfile path: ' message]);
self.logLevel_ = logging.logging.OFF;
end
self.fullpath = logPath;
end
function setCommandWindowLevel(self, level)
self.commandWindowLevel = level;
end
function setLogLevel(self, level)
self.logLevel = level;
end
function setBufferingSize(self, bufferSize)
self.bufferingSize = bufferSize;
end
function tf = ignoreLogging(self)
tf = self.commandWindowLevel_ == self.OFF && self.logLevel_ == self.OFF;
end
function trace(self, message)
[caller_name, ~] = self.getCallerInfo(self);
self.writeLog(self.TRACE, caller_name, message);
end
function debug(self, message)
[caller_name, ~] = self.getCallerInfo(self);
self.writeLog(self.DEBUG, caller_name, message);
end
function info(self, message)
[caller_name, ~] = self.getCallerInfo(self);
self.writeLog(self.INFO, caller_name, message);
end
function warn(self, message)
[caller_name, ~] = self.getCallerInfo(self);
self.writeLog(self.WARNING, caller_name, message);
end
function error(self, message)
[caller_name, ~] = self.getCallerInfo(self);
self.writeLog(self.ERROR, caller_name, message);
end
function critical(self, message)
[caller_name, ~] = self.getCallerInfo(self);
self.writeLog(self.CRITICAL, caller_name, message);
end
function self = logging(name, varargin)
levelkeys = self.levels.keys;
self.level_numbers = containers.Map(...
self.levels.values, levelkeys);
levelkeys = cell2mat(self.levels.keys);
self.level_range = [min(levelkeys), max(levelkeys)];
p = inputParser();
p.addRequired('name', @ischar);
p.addParameter('path', '', @ischar);
p.addParameter('logLevel', self.logLevel);
p.addParameter('commandWindowLevel', self.commandWindowLevel);
p.addParameter('datefmt', self.datefmt_);
p.addParameter('bufferingSize', 1);
p.parse(name, varargin{:});
r = p.Results;
self.name = r.name;
self.commandWindowLevel = r.commandWindowLevel;
self.datefmt = r.datefmt;
self.setBufferingSize(r.bufferingSize);
if ~isempty(r.path)
self.setFilename(r.path); % Opens the log file.
self.logLevel = r.logLevel;
else
self.logLevel_ = logging.logging.OFF;
end
% Use terminal logging if swing is disabled in matlab environment.
swingError = javachk('swing');
self.using_terminal = (~ isempty(swingError) && strcmp(swingError.identifier, 'MATLAB:javachk:thisFeatureNotAvailable')) || ~desktop('-inuse');
end
function delete(self)
if self.logfid > -1
fclose(self.logfid);
end
end
function writeLog(self, level, caller, message)
level = self.getLevelNumber(level);
if self.commandWindowLevel_ <= level || self.logLevel_ <= level
timestamp = datestr(now, self.datefmt_);
levelStr = logging.logging.levels(level);
logline = sprintf(self.logfmt, caller, timestamp, levelStr, self.getMessage(message));
end
if self.commandWindowLevel_ <= level
if self.using_terminal
level_color = self.level_colors(level);
else
level_color = self.level_colors(logging.logging.INFO);
end
fprintf(self.logcolors(level_color), logline);
end
if self.logLevel_ <= level && self.logfid > -1
self.bufferedMessages_{self.bufferCursor_} = logline;
if self.bufferCursor_ == numel(self.bufferedMessages_)
cellfun(@(x) fprintf(self.logfid, x), self.bufferedMessages_);
self.bufferCursor_ = 1;
else
self.bufferCursor_ = self.bufferCursor_ + 1;
end
end
end
function set.datefmt(self, fmt)
try
datestr(now(), fmt);
catch
error('Invalid date format');
end
self.datefmt_ = fmt;
end
function fmt = get.datefmt(self)
fmt = self.datefmt_;
end
function set.logLevel(self, level)
level = self.getLevelNumber(level);
if level > logging.logging.OFF || level < logging.logging.ALL
error('invalid logging level');
end
self.logLevel_ = level;
end
function level = get.logLevel(self)
level = self.logLevel_;
end
function set.commandWindowLevel(self, level)
self.commandWindowLevel_ = self.getLevelNumber(level);
end
function level = get.commandWindowLevel(self)
level = self.commandWindowLevel_;
end
function set.bufferingSize(self, bufferSize)
validateattributes(bufferSize, {'numeric'}, {'scalar', 'integer', 'nonzero', 'positive'});
n = numel(self.bufferedMessages_);
if bufferSize > n
self.bufferedMessages_ = [self.bufferedMessages_ cell(1, bufferSize - n)];
else
self.bufferedMessages_ = self.bufferedMessages_(1:bufferSize);
end
end
function bufferSize = get.bufferingSize(self)
bufferSize = numel(self.bufferedMessages_);
end
end
methods (Hidden)
function level = getLevelNumber(self, level)
% LEVEL = GETLEVELNUMBER(LEVEL)
%
% Converts charecter-based level names to level numbers
% used internally by logging.
%
% If given a number, it makes sure the number is valid
% then returns it unchanged.
%
% This allows users to specify levels by name or number.
if isinteger(level) && self.level_range(1) <= level && level <= self.level_range(2)
return
else
level = self.level_numbers(level);
end
end
function message = getMessage(~, message)
if isa(message, 'function_handle')
message = message();
end
[rows, ~] = size(message);
if rows > 1
message = sprintf('\n %s', evalc('disp(message)'));
end
end
end
end