0001 function log_level = eidors_msg( message, varargin )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 persistent last_msg
0060
0061 if ischar(message) && strcmp(message,'UNIT_TEST'); do_unit_test; return; end
0062
0063 if ischar(message) && strcmp(message, 'log_level')
0064 log_level = process_log_level(varargin{:});
0065 return;
0066 end
0067
0068 if ischar(message) && strcmp(message, 'last_msg')
0069 log_level = last_msg;
0070 return;
0071 end
0072
0073 if nargin==1
0074 args= {};
0075 level= 2;
0076 else
0077 level= varargin{ nargin-1 };
0078 args= varargin( 1:nargin-2 );
0079 end
0080
0081 [log_level] = get_levels;
0082
0083 if level > log_level
0084 return
0085 end
0086
0087 for i= 1:length(args)
0088 if isa( args{i}, 'function_handle')
0089 args{i} = func2str(args{i});
0090 end
0091 end
0092
0093
0094 ver= eidors_obj('interpreter_version');
0095 if ver.ver>=7; fid= 1;
0096 else ; fid= 2; end
0097
0098
0099
0100
0101
0102
0103
0104 if ~ischar(message)
0105 var = inputname(1);
0106 message = [var ' = ' num2str(message)];
0107 end
0108
0109
0110
0111
0112 if length(message)>1
0113 if length(message)>=2 && strcmp(message(1:2),'@@');
0114 if length(message)==2; msg_extra = '';
0115 else msg_extra = [':', message(3:end)];
0116 end
0117 [file fun] = get_caller();
0118 dbs = dbstack;
0119 if length(message) > 2 && message(3) == '@'
0120 if length(message) == 3
0121 msg_extra = '';
0122 else
0123 msg_extra(2) = [];
0124 end
0125 message = sprintf('%s/%s%s', file, fun, msg_extra);
0126 else
0127 message = sprintf('%s%s', file , msg_extra);
0128 end
0129 end
0130 s = dbstack();
0131 last_msg = sprintf('%s:%d %s', s(2).file, s(2).line, message);
0132
0133 string= [sprintf('%c',' ' * ones(1,level-1)), ...
0134 'EIDORS:[',message,']\n'];
0135
0136 else
0137 string= message;
0138 end
0139
0140 fprintf(fid, string, args{:});
0141 if exist('OCTAVE_VERSION'); fflush(fid); end
0142 eidors_objects.last_message_size= 0;
0143
0144
0145 function log_level = process_log_level(varargin)
0146 global eidors_objects
0147 if isempty(varargin)
0148
0149 log_level = eidors_objects.log_level;
0150 return
0151 end
0152 if ischar(varargin{1}) && ~isempty(str2num(varargin{1}))
0153 varargin{1} = str2num(varargin{1});
0154 end
0155 if isnumeric(varargin{1})
0156 log_level = eidors_objects.log_level;
0157 switch nargin
0158 case 1
0159
0160 eidors_objects.log_level = varargin{1};
0161 case 2
0162
0163 caller = get_caller;
0164 custom = eidors_objects.log_level_custom;
0165 if isempty(custom), return, end
0166 idx = find(ismember(caller, custom.names), 1);
0167 if isempty(idx), idx = length(custom.levels) + 1; end
0168 eidors_objects.log_level_custom.names{idx} = caller;
0169 eidors_objects.log_level_custom.levels(idx) = varargin{1};
0170 otherwise
0171 error('Wrong number of inputs');
0172
0173 end
0174 elseif ischar(varargin{1}) && strcmp(varargin{1}, 'reset')
0175 log_level = eidors_objects.log_level;
0176
0177 switch nargin
0178 case 1
0179
0180 eidors_objects.log_level_custom.names = {};
0181 eidors_objects.log_level_custom.levels = [];
0182 case 2
0183 switch varargin{2}
0184 case 'all'
0185
0186 eidors_objects.log_level_custom.names = {};
0187 eidors_objects.log_level_custom.levels = [];
0188 otherwise
0189 caller = get_caller;
0190 idx = find(ismember(caller, ...
0191 eidors_objects.log_level_custom.names{1}), 1);
0192 eidors_objects.log_level_custom.names(idx) = [];
0193 eidors_objects.log_level_custom.levels(idx) = [];
0194 end
0195 otherwise
0196 error('Wrong number of inputs');
0197 end
0198
0199 else
0200 error('Wrong input, consult help.');
0201
0202 end
0203
0204
0205 function [file fun] = get_caller
0206 dbs = dbstack;
0207 file = dbs(3).file;
0208 idx = find(file == '.', 1, 'last');
0209 if isempty(idx), idx = length(file)+1; end
0210 file = file(1:idx-1);
0211
0212 fun = dbs(3).name;
0213
0214 function [log_level] = get_levels
0215 global eidors_objects
0216 try
0217 custom = eidors_objects.log_level_custom;
0218 catch
0219 custom.names = {};
0220 custom.levels = [];
0221 eidors_objects.log_level_custom = custom;
0222 end
0223 if ~isempty(custom)
0224
0225 file = get_caller;
0226
0227 idx = find(ismember(file, custom.names));
0228 if ~isempty(idx)
0229 log_level = custom.levels(idx);
0230 return
0231 end
0232 end
0233 try
0234 log_level= eidors_objects.log_level;
0235 catch
0236 log_level= 2;
0237 eidors_objects.log_level= log_level;
0238 end
0239
0240 function do_unit_test
0241 ll= eidors_msg('log_level');
0242 eidors_msg('log_level',5);
0243 eidors_msg('l1',1); eidors_msg('l2',2); eidors_msg('l3',3); eidors_msg('l4',4);
0244
0245 eidors_msg('log_level',1);
0246 eidors_msg('l1',1); eidors_msg('l2',2); eidors_msg('l3',3); eidors_msg('l4',4);
0247
0248 eidors_msg('@@',1);
0249 eidors_msg('@@@',1);
0250 eidors_msg('@@ a message',1);
0251 eidors_msg('@@@ a message',1);
0252 eidors_msg('a @@ message',1);
0253 eidors_msg('a @@@ message',1);
0254 extra_caller;
0255
0256
0257 eidors_msg('log_level',2);
0258 eidors_msg('FAIL',3);
0259 eidors_msg('log_level',1, 'eidors_msg');
0260 eidors_msg('PASS',1);
0261 eidors_msg('FAIL',2);
0262 eidors_msg('log_level',3, 'eidors_msg');
0263 eidors_msg('PASS',3);
0264 eidors_msg('FAIL',4);
0265 eidors_msg('log_level','reset','eidors_msg');
0266 eidors_msg('PASS',2);
0267 eidors_msg('FAIL',3);
0268
0269 eidors_msg('log_level','reset');
0270 eidors_msg('log_level','reset','all');
0271
0272 eidors_msg('log_level',ll);
0273
0274
0275 function extra_caller
0276 eidors_msg('@@@ a message from extra_caller',1);
0277 eidors_msg('@@ a shorter message from extra caller',1);
0278