-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlogging.m
85 lines (67 loc) · 2.2 KB
/
logging.m
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
classdef logging < handle
% Created by Alexander Fyrdahl <[email protected]>
% Inspired by log4m by Luke Winslow <[email protected]>
properties(Access = protected)
filename;
end
methods (Static)
function obj = createLog(filename)
if nargin < 1
filename = 'fire.log';
end
persistent thisObj;
if isempty(thisObj) || ~isvalid(thisObj)
thisObj = logging(filename);
end
obj = thisObj;
end
end
methods
function trace(self,varargin)
writeLog(self,0,sprintf(varargin{:}));
end
function debug(self,varargin)
writeLog(self,1,sprintf(varargin{:}));
end
function info(self,varargin)
writeLog(self,2,sprintf(varargin{:}));
end
function warn(self,varargin)
writeLog(self,3,sprintf(varargin{:}));
end
function error(self,varargin)
writeLog(self,4,sprintf(varargin{:}));
end
end
methods (Access = private)
function self = logging(filename)
self.filename = filename;
end
function writeLog(self,level,message)
switch level
case 1
levelStr = 'DEBUG';
case 2
levelStr = 'INFO';
case 3
levelStr = 'WARN';
case 4
levelStr = 'ERROR';
otherwise
levelStr = 'TRACE';
end
% Write to log file,
fid = fopen(self.filename,'a');
fprintf(fid,'%s %s - %s\n' ...
, datestr(now,'yyyy-mm-dd HH:MM:SS,FFF') ...
, levelStr ...
, message);
fclose(fid);
% but output to terminal as well
fprintf('%s %s - %s\n' ...
, datestr(now,'yyyy-mm-dd HH:MM:SS,FFF') ...
, levelStr ...
, message);
end
end
end