eidors_msg

PURPOSE ^

EIDORS_MSG eidors progress and status messages

SYNOPSIS ^

function log_level = eidors_msg( message, varargin )

DESCRIPTION ^

EIDORS_MSG eidors progress and status messages

  EIDORS_MSG(STR, LVL) prints 'EIDORS:[ STR ]' if current log_level <= LVL
     If the message starts with '@@@' then the characters are replaced
     with the calling function's name. Default LVL = 2.

  EIDORS_MSG(STR, LVL, ...) uses STR as format string for fprintf passing
     to it any additional arguments.

  LVL = EIDORS_MSG('log_level') returns the current log_level

  EIDORS_MSG('log_level', LVL) sets the log_level for all of EIDORS
  or EIDORS_MSG  log_level  LVL

  EIDORS_MSG('log_level', LVL, FNAME) sets a custom log_level for m-file
     FNAME (provide without extension)

  EIDORS_MSG('log_level','reset', FNAME) resets the log_level for m-file
     FNAME to the default level

  EIDORS_MSG('log_level','reset')
  EIDORS_MSG('log_level','reset','all') resets all custom log-level
     settings to default

  EIDORS_MSG('last_msg') return last message

  Meanings of levels:
     0 => keep quiet (no messages should be level=0)
     1 => important messages
     2 => most messages
     3 => detailed information%
     4 => very detailed information

  Examples:
  eidors_msg('mission accomplished; time to relax', 1) prints
     >> EIDORS:[ mission accomplished; time to relax ]'
       if log_level <=1
  less important messages have higher levels

  eidors_msg('did %d of %s at %f', 2, 'stuff', sqrt(2), 1)
     >> EIDORS:[ did 2 of stuff at 1.414214 ]

  eidors_msg('@@');
     >> EIDORS:[calling_file]

  eidors_msg('@@@');
     >> EIDORS:[calling_file/function]

  eidors_msg('@@@ a message',1);
     >> EIDORS:[calling_file/function: a message]

  See also EIDORS_DEBUG, EIDORS_CACHE, PROGRESS_MSG.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function log_level = eidors_msg( message, varargin )
0002 %EIDORS_MSG eidors progress and status messages
0003 %
0004 %  EIDORS_MSG(STR, LVL) prints 'EIDORS:[ STR ]' if current log_level <= LVL
0005 %     If the message starts with '@@@' then the characters are replaced
0006 %     with the calling function's name. Default LVL = 2.
0007 %
0008 %  EIDORS_MSG(STR, LVL, ...) uses STR as format string for fprintf passing
0009 %     to it any additional arguments.
0010 %
0011 %  LVL = EIDORS_MSG('log_level') returns the current log_level
0012 %
0013 %  EIDORS_MSG('log_level', LVL) sets the log_level for all of EIDORS
0014 %  or EIDORS_MSG  log_level  LVL
0015 %
0016 %  EIDORS_MSG('log_level', LVL, FNAME) sets a custom log_level for m-file
0017 %     FNAME (provide without extension)
0018 %
0019 %  EIDORS_MSG('log_level','reset', FNAME) resets the log_level for m-file
0020 %     FNAME to the default level
0021 %
0022 %  EIDORS_MSG('log_level','reset')
0023 %  EIDORS_MSG('log_level','reset','all') resets all custom log-level
0024 %     settings to default
0025 %
0026 %  EIDORS_MSG('last_msg') return last message
0027 %
0028 %  Meanings of levels:
0029 %     0 => keep quiet (no messages should be level=0)
0030 %     1 => important messages
0031 %     2 => most messages
0032 %     3 => detailed information%
0033 %     4 => very detailed information
0034 %
0035 %  Examples:
0036 %  eidors_msg('mission accomplished; time to relax', 1) prints
0037 %     >> EIDORS:[ mission accomplished; time to relax ]'
0038 %       if log_level <=1
0039 %  less important messages have higher levels
0040 %
0041 %  eidors_msg('did %d of %s at %f', 2, 'stuff', sqrt(2), 1)
0042 %     >> EIDORS:[ did 2 of stuff at 1.414214 ]
0043 %
0044 %  eidors_msg('@@');
0045 %     >> EIDORS:[calling_file]
0046 %
0047 %  eidors_msg('@@@');
0048 %     >> EIDORS:[calling_file/function]
0049 %
0050 %  eidors_msg('@@@ a message',1);
0051 %     >> EIDORS:[calling_file/function: a message]
0052 %
0053 %  See also EIDORS_DEBUG, EIDORS_CACHE, PROGRESS_MSG.
0054 
0055 % (C) 2005-2014 Andy Adler and Bartlomiej Grychtol
0056 % License: GPL version 2 or version 3
0057 % $Id: eidors_msg.m 6748 2024-04-08 19:27:51Z bgrychtol $
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 % It makes sense to print to stderr (fid=2), but matlab>7 prints this in red
0094 ver= eidors_obj('interpreter_version');
0095 if ver.ver>=7; fid= 1;
0096 else ;         fid= 2; end
0097 
0098 
0099 %try, lms= eidors_objects.last_message_size;
0100 %  if lms>0; fprintf(fid,'%c', 8*ones(lms,1)); end
0101 %end
0102 
0103 % deal with variables
0104 if ~ischar(message)
0105    var = inputname(1);
0106    message = [var ' = ' num2str(message)];
0107 end
0108 
0109 
0110 % Need to do twice to interpret text in message
0111 % message= sprintf(message, args{:} );
0112 if length(message)>1 % single characters are just for progress
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       % eidors_msg('log_level');
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             % eidors_msg('log_level', 2);
0160             eidors_objects.log_level = varargin{1};
0161          case 2
0162             % eidors_msg('log_level', 1, fname);
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             % eidors_msg('log_level','reset');
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                   % eidors_msg('log_level','reset', 'all');
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    start = find(file == filesep, 1, 'last');
0209    if isempty(start), start = 0; end
0210    idx = find(file == '.', 1, 'last');
0211    if isempty(idx), idx = length(file)+1; end
0212    file = file((start+1):idx-1);
0213 
0214    fun =  dbs(3).name;
0215 
0216 function [log_level] = get_levels
0217    global eidors_objects
0218    try
0219       custom = eidors_objects.log_level_custom;
0220    catch
0221       custom.names = {};
0222       custom.levels = [];
0223       eidors_objects.log_level_custom = custom;
0224    end
0225    if ~isempty(custom)
0226       % find caller file name
0227       file = get_caller;
0228       % check for custom flag
0229       idx = find(ismember(file, custom.names));
0230       if ~isempty(idx)
0231          log_level = custom.levels(idx);
0232          return
0233       end
0234    end
0235    try
0236       log_level= eidors_objects.log_level;
0237    catch
0238       log_level= 2; % default;
0239       eidors_objects.log_level= log_level;
0240    end
0241 
0242 function do_unit_test
0243    ll= eidors_msg('log_level');
0244    eidors_msg('log_level',5);
0245    eidors_msg('l1',1); eidors_msg('l2',2); eidors_msg('l3',3); eidors_msg('l4',4);
0246 
0247    eidors_msg('log_level',1);
0248    eidors_msg('l1',1); eidors_msg('l2',2); eidors_msg('l3',3); eidors_msg('l4',4);
0249 
0250    eidors_msg('@@',1);
0251    eidors_msg('@@@',1);
0252    eidors_msg('@@ a message',1);
0253    eidors_msg('@@@ a message',1);
0254    eidors_msg('a @@ message',1);
0255    eidors_msg('a @@@ message',1);
0256    extra_caller;
0257 
0258    % test custom levels
0259    eidors_msg('log_level',2);
0260    eidors_msg('FAIL',3);
0261    eidors_msg('log_level',1, 'eidors_msg');
0262    eidors_msg('PASS',1);
0263    eidors_msg('FAIL',2);
0264    eidors_msg('log_level',3, 'eidors_msg');
0265    eidors_msg('PASS',3);
0266    eidors_msg('FAIL',4);
0267    eidors_msg('log_level','reset','eidors_msg');
0268    eidors_msg('PASS',2);
0269    eidors_msg('FAIL',3);
0270    % just for fun
0271    eidors_msg('log_level','reset');
0272    eidors_msg('log_level','reset','all');
0273 
0274    eidors_msg('log_level',ll);
0275 
0276 
0277 function extra_caller
0278    eidors_msg('@@@ a message from extra_caller',1);
0279    eidors_msg('@@ a shorter message from extra caller',1);
0280

Generated on Sun 29-Dec-2024 11:41:59 by m2html © 2005