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