rbmatlab 0.10.01
|
00001 function r = verbose( level, message, messageId) 00002 % function r = verbose( level, message, messageId ) 00003 % This function displays messages depending on a message-id and/or a level. 00004 % Aditionally you can set/reset the level-filer and add ore remove message-ids 00005 % to the include and exclude lists. 00006 % 00007 % When messages will be displayed? 00008 % -------------------------------- 00009 % A Messages only will be diplayed only then if one of the following situation applies. 00010 % 1. The message-id is listet in the include list 00011 % 2. The messages level is lower or equal to the previously set and the message-id don't 00012 % appear in the exclude list 00013 % 00014 % Basic examples: 00015 % --------------- 00016 % To use the basic operation of verbose, only one or two arguments, 00017 % the level and the message are necessary. 00018 % 00019 % three ways to set the verbosity level e.g. to 10 00020 % 1. > verbose(10); 00021 % 2. > verbose('level',10); 00022 % 3. > oldVerbLev = verbose(10); % change level to 10 00023 % > one_of_my_functions(); % ... some code ... 00024 % > verbose(oldVerbLevel); % reset level to value before changed 00025 % 00026 % get the current verbosity level 00027 % 1. > level = verbose(); 00028 % 2. > level = verbose('level'); 00029 % 00030 % message output (verbosity level previously set to 10) 00031 % > verbose(10, 'Hello World!'); % message will be displayed 00032 % > verbose(11, 'Hello World!'); % ... won't ... 00033 % > verbose(1, 'Hello World!'); % ... will ... 00034 % 00035 % Extended examples: 00036 % ------------------ 00037 % The third argument, the message-id enables you to specify more precisely 00038 % what should be displayed or not. 00039 % 00040 % add/remove one or more message-ids to the include-/excludelist 00041 % > verbose('addInclude', 'RB:ERROR'); % add 'RB:ERROR' to the include list 00042 % > verbose('addInclude', {'RB:WARNING', 'RB:INFO'}); % add 'RB:WARNING' and RB:INFO ... 00043 % > verbose('include'); % return includelist 00044 % % e.g. {'RB:ERROR', 'RB:WARNING', 'RB:INFO'} 00045 % > verbose('addExclude', {'RB:INFO','RB:UNKNOWN'}); % add 'RB:INFO','RB:UNKNOWN' to excludelist 00046 % > verbose('delInclude', {'RB:ERROR','RB:INFO'}); % remove 'RB:ERROR' and 'RB:INFO' 00047 % > verbose('exclude', {}); % clear exclude list 00048 % 00049 % print messages 00050 % > verbose(10); 00051 % > verbose('include', {'RB:SURPRISE1'}); 00052 % > verbose('exclude', {'RB:SURPRISE2'}); 00053 % > verbose(); 00054 % > verbose(1, 'Hello World!', 'RB:INFO'); 00055 % > verbose(99, 'Hello What Happened', 'RB:SURPRISE1'); % displayed, cause RB:SURPRISE1 is in includelist 00056 % > verbose(0, 'Surprise for you?'); % no, you don't get it, it's excluded 00057 00058 00059 persistent VERBOSE; 00060 00061 % initialize structure VERBOSE 00062 if isempty( VERBOSE ) 00063 VERBOSE.level = 0; 00064 VERBOSE.include = {}; 00065 VERBOSE.exclude = {}; 00066 VERBOSE.addInclude = @(x) VERBOSE_addInclude(x); 00067 VERBOSE.delInclude = @(x) VERBOSE_delInclude(x); 00068 VERBOSE.addExclude = @(x) VERBOSE_addExclude(x); 00069 VERBOSE.delExclude = @(x) VERBOSE_delExclude(x); 00070 end; 00071 00072 if (nargin==0) 00073 r = VERBOSE.level; 00074 elseif (nargin>0) 00075 if isnumeric( level ) 00076 if nargin==2 00077 % filter by level 00078 if ( level <= VERBOSE.level ) 00079 disp( message ); 00080 end; 00081 return; 00082 elseif nargin==3 00083 [st,i] = dbstack; 00084 if (length(st)>1) 00085 [paths,fileName] = fileparts(st(2).file); 00086 funcName = st(2).name; 00087 lineNo = num2str(st(2).line); 00088 if isempty(messageId) 00089 messageId = sprintf('RB:%s:UNKNOWN',fileName); 00090 end; 00091 message = sprintf('%s file=%s func=%s line=%s\n ==> ''%s''\r\n', ... 00092 messageId, fileName, funcName, lineNo, message); 00093 else 00094 if isempty(messageId) 00095 messageId = 'RB:UNKNOWN:UNKNOWN'; 00096 message = sprintf('RB:UNKNOWN:UNKNOWN\r\n ==> ''%s''\r\n', message); 00097 else 00098 message = sprintf('%s\r\n ==> ''%s''\r\n', messageId, message); 00099 end; 00100 end; 00101 % filter by messageId and level 00102 if (( ~isempty(VERBOSE.include ) && ( any(strcmp(messageId, VERBOSE.include )))) || ... 00103 ((level<=VERBOSE.level) && ( isempty(VERBOSE.exclude ) || ( ~any(strcmp(messageId, VERBOSE.exclude )))))) 00104 disp( message ); 00105 end; 00106 return; 00107 elseif ( nargin == 1 ) 00108 r = VERBOSE.level; 00109 VERBOSE.level = level; 00110 return; 00111 end; 00112 elseif ischar(level) 00113 if isfield(VERBOSE, level) 00114 if isa(VERBOSE.(level), 'function_handle') 00115 % call function field 00116 r = VERBOSE.(level)(message); 00117 return; 00118 else 00119 % set value of field 00120 r = VERBOSE.(level); 00121 if nargin==2 00122 VERBOSE.(level) = message; 00123 end; 00124 end; 00125 else 00126 verbose( 0, 'first argument is a unknown field.', 'RB:VERBOSE:ARGERR'); 00127 end; 00128 else 00129 verbose( 0, 'first argument should be of type numeric or character.', 'RB:VERBOSE:ARGERR'); 00130 end; 00131 end; 00132 00133 function r = VERBOSE_addExclude(c) 00134 % addExclude 00135 r = VERBOSE.exclude; 00136 VERBOSE.exclude = union(VERBOSE.exclude, c); 00137 end 00138 00139 function r = VERBOSE_delExclude(c) 00140 % delExclude 00141 r = VERBOSE.exclude; 00142 VERBOSE.exclude = setdiff(VERBOSE.exclude, c); 00143 end 00144 00145 function r = VERBOSE_addInclude(c) 00146 % addInclude 00147 r = VERBOSE.include; 00148 VERBOSE.include = union(VERBOSE.include, c); 00149 end 00150 00151 function r = VERBOSE_delInclude(c) 00152 % delInclude 00153 r = VERBOSE.include; 00154 VERBOSE.include = setdiff(VERBOSE.include, c); 00155 end 00156 00157 end 00158