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

  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:[eidors_msg]

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

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

  See also EIDORS_DEBUG, EIDORS_CACHE.

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 %  Meanings of levels:
0027 %     0 => keep quiet (no messages should be level=0)
0028 %     1 => important messages
0029 %     2 => most messages
0030 %     3 => detailed information%
0031 %     4 => very detailed information
0032 %
0033 %  Examples:
0034 %  eidors_msg('mission accomplished; time to relax', 1) prints
0035 %     >> EIDORS:[ mission accomplished; time to relax ]'
0036 %       if log_level <=1
0037 %  less important messages have higher levels
0038 %
0039 %  eidors_msg('did %d of %s at %f', 2, 'stuff', sqrt(2), 1)
0040 %     >> EIDORS:[ did 2 of stuff at 1.414214 ]
0041 %
0042 %  eidors_msg('@@');
0043 %     >> EIDORS:[eidors_msg]
0044 %
0045 %  eidors_msg('@@@');
0046 %     >> EIDORS:[eidors_msg/do_unit_test]
0047 %
0048 %  eidors_msg('@@@ a message',1);
0049 %     >> EIDORS:[eidors_msg/do_unit_test: a message]
0050 %
0051 %  See also EIDORS_DEBUG, EIDORS_CACHE.
0052 
0053 % (C) 2005-2014 Andy Adler and Bartlomiej Grychtol
0054 % License: GPL version 2 or version 3
0055 % $Id: eidors_msg.m 5111 2015-06-14 12:55:54Z aadler $
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 % It makes sense to print to stderr (fid=2), but matlab>7 prints this in red
0085 ver= eidors_obj('interpreter_version');
0086 if ver.ver>=7; fid= 1;
0087 else ;         fid= 2; end
0088 
0089 
0090 %try, lms= eidors_objects.last_message_size;
0091 %  if lms>0; fprintf(fid,'%c', 8*ones(lms,1)); end
0092 %end
0093 
0094 % deal with variables
0095 if ~ischar(message)
0096    var = inputname(1);
0097    message = [var ' = ' num2str(message)];
0098 end
0099 
0100 
0101 % Need to do twice to interpret text in message
0102 % message= sprintf(message, args{:} );
0103 if length(message)>1 % single characters are just for progress
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       % eidors_msg('log_level');
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             % eidors_msg('log_level', 2);
0148             eidors_objects.log_level = varargin{1};
0149          case 2
0150             % eidors_msg('log_level', 1, fname);
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             % eidors_msg('log_level','reset');
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                   % eidors_msg('log_level','reset', 'all');
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       % find caller file name
0213       file = get_caller;
0214       % check for custom flag
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; % default;
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    % test custom levels
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    % just for fun
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

Generated on Tue 31-Dec-2019 17:03:26 by m2html © 2005