-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoqc_LOG.m
170 lines (154 loc) · 7.75 KB
/
hoqc_LOG.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
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
classdef hoqc_LOG < handle
%{
examples of use:
mylog = hoqc_LOG('logje.txt','reset',true) ; % log to file, reset, close after each write
mylog.set_types('info',false); % do not show INFO messages
mylog.write('info', ... % message will not be shown (see prev. line)
'function %s called input %7.3f','f1',exp(1));
mylog.write('xxx', ... % message will not be shown (unknown type:
'function %s called input %7.3f','f2',exp(2)); % i.e not one of INFO, DEBUG or ERROR
mylog.write('debug', ... % message will be shown (default for
'function %s called input %7.3f','f3',exp(3)); % INFO, DEBUG and ERROR is true)
mylog.set_types('info',true,'xxx',true); % show INFO and XXX messages
mylog.write('info', ... % message will now be shown
'function %s called input %7.3f','f4',exp(4));
mylog.write('xxx', ... % message will now be shown
'function %s called input %7.3f','f5',exp(5));
mylog.closefile() % close logfile
%}
properties (GetAccess = private, Constant = true)
version = '0.1' ;
end
properties (GetAccess = private)
logfile = '' ; % name logfile (set by constructor)
logopen = 0 ; % file handle logfile (set by constructor)
cl_aw = false ; % close after write? (set by constructor)
ts = true ; % timestamp to insert? (set by constructor)
msg_types = {} ; % message types (set by set_types)
msg_tf = true ; % display types? (set by set_types)
end
methods (Access = public)
function obj = hoqc_LOG(varargin) % constructor
p = inputParser;
addRequired(p,'logfile',@ischar); % name logfile
addParameter(p,'reset',false,@islogical) ; % reset logfile ?
addParameter(p,'cl_aw',false,@islogical) ; % close after write?
addParameter(p,'ts',true,@islogical) ; % timestamp to insert?
parse(p,varargin{:}) % parse inputs
obj.logfile = p.Results.logfile ; % name logfile
obj.logopen = 0 ; % file handle 0 (not open yet)
reset = p.Results.reset ; % reset logfile ?
obj.cl_aw = p.Results.cl_aw ; % close after write?
obj.ts = p.Results.ts ; % timestamp to insert?
obj.msg_types = {'info','debug','error'}; % message types
obj.msg_tf = {true, true, true}; % display types ?
if reset && numel(obj.logfile) > 0
writefile(obj,'',reset) % reset (empty) logfile
end
end
function version = getversion(obj)
version = obj.version;
end
function logfile = getlogfile(obj)
logfile = obj.logfile;
end
function set_types(obj,varargin) % indicate which types will be printed
types = varargin ;
ntypes = numel(types);
if ntypes == 0, return, end
if mod(numel(types),2) == 1
error('hoqc_LOG.set_types: not enough arguments')
end
for i=1:numel(types)/2
set_type(obj, ... % set set type to false or true
types{2*i-1},types{2*i});
end
end
function tf = get_type(obj,type) % inquire if type will be printed
type = lower(type); % argument to lower case
[~,ix] = ismember(type,obj.msg_types) ; % lookup
if ix == 0
tf = false ; % not found: not to print
else
tf = obj.msg_tf{ix} ; % found: return boolean
end
end
function write(obj,type,msg,varargin) % write message (if type fits)
if get_type(obj,type) == false
return;
end
if obj.ts == true
dt = datestr(now, ... % formatted current time
'yyyy-mm-dd HH:MM:SS.FFF');
else
dt = '' ; % no date_time included in message
end
msg_out = sprintf('%-7s%s - %s', ... % combine type, timestamp and message
upper(type),dt,msg) ;
if numel(varargin) ~= sum(msg_out=='%') % arguments correspond with message?
error(['hoqc_LOG.write: ', ... % display error if not
'incorrect number of arguments'])
else
msg_out=sprintf(msg_out,varargin{:}) ; % insert arguments in message
end
if numel(obj.logfile) == 0
fprintf('%s\n',msg_out);
else
writefile(obj,msg_out) ;
end
end
function closefile(obj)
if obj.logopen > 0
fclose(obj.logopen);
end
obj.logopen = 0;
end
end
methods (Access = private)
function set_type(obj,type1,tf1) % indicate which types will be printed
[~,ix] = ismember(type1,obj.msg_types) ;
if ix > 0
obj.msg_tf{ix} = tf1; % type1 found: change boolean
else
obj.msg_types = [obj.msg_types, type1]; % add type1 to array
obj.msg_tf = [obj.msg_tf, tf1]; % with corresponding boolean
end
end
function writefile(obj,msg,varargin)
defoptArgs = {false} ; % default values for reset
optArgs = ... % merge specified and default values
hoqc_LOG.setOptArgs(varargin,defoptArgs) ;
reset = optArgs{1} ; % reset file (t) or append (f)
if reset
oparm = 'wt' ; % reset
else
oparm = 'at' ; % append
end
if obj.logopen == 0
obj.logopen = fopen(obj.logfile,oparm);
if obj.logopen == 0
error(['hoqc_LOG.write: ', ... % display error if file could not be opened
'file %s could not be opened'], ...
obj.logfile)
end
end
if reset == false
fprintf(obj.logopen,'%s\n',msg);
end
if reset || (obj.cl_aw == true)
closefile(obj);
end
end
end
methods (Static, Access = private)
function defArgs = setOptArgs(a,defArgs)
% set non-specified optional parameters to default values
% idea from Omid Khanmohamadi on matlabcentral
% a : arguments passed with varargin
% defArgs : on input default arguments
% on output default argument overwritten by the specified ones (if not empty)
empty_a = cellfun(@(x)isequal(x,[]),a); % indicate a that are not specified (empty)
[defArgs{~empty_a}] = a{~empty_a}; % replace defaults by non-empty one
end
end
end