Home > atmlab > handy > logtext.m

logtext

PURPOSE ^

logtext Log text to fid along with callers name+lineno

SYNOPSIS ^

function logtext(fid, text, varargin)

DESCRIPTION ^

 logtext Log text to fid along with callers name+lineno

 logtext is a logging function that writes text to the indicated file
 descriptor along with the filename and the line number of the caller.
 If writing to stdout or stderr and to the java desktop, it will make the
 filename+line number clickable. If atmlab('VERBOSITY') is zero, it will
 not write anything at all.

 FORMAT logtext(fid, text, ...)

 IN

   fid  number  File descriptor to write to
   text string  Text to write
   ...  ...     All subsequent arguments are passed to fprintf

 $Id$

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

logtext.m

SOURCE CODE ^

0001 function logtext(fid, text, varargin)
0002 
0003 % logtext Log text to fid along with callers name+lineno
0004 %
0005 % logtext is a logging function that writes text to the indicated file
0006 % descriptor along with the filename and the line number of the caller.
0007 % If writing to stdout or stderr and to the java desktop, it will make the
0008 % filename+line number clickable. If atmlab('VERBOSITY') is zero, it will
0009 % not write anything at all.
0010 %
0011 % FORMAT logtext(fid, text, ...)
0012 %
0013 % IN
0014 %
0015 %   fid  number  File descriptor to write to
0016 %   text string  Text to write
0017 %   ...  ...     All subsequent arguments are passed to fprintf
0018 %
0019 % $Id$
0020 
0021 % TODO:
0022 %   - should this use atmlab('VERBOSITY') or atmlab('DEBUG')?
0023 %   - when stdout or stderr, try to check if output is matlab java terminal or
0024 %   just a console. Is this possible?
0025 
0026 if ~atmlab('VERBOSITY')
0027     return
0028 end
0029 
0030 haspar = ~isempty(ver('distcomp')) && ~isempty(getCurrentTask());
0031 
0032 ST = dbstack;
0033 if length(ST) < 2
0034     lineno = -1;
0035     name = '(none)';
0036 else
0037     lineno = ST(2).line;
0038     f = ST(2).file;
0039     name = ST(2).name;
0040 end
0041 
0042 % ugliness: try to get class/self.name in caller
0043 try
0044     cl = evalin('caller', 'class(self)');
0045     nm = evalin('caller', 'self.name');
0046     showcls = true;
0047 catch ME % never mind
0048     showcls = false;
0049 end   
0050 
0051 fprintf(fid, '%s:', datestr(now, 'dd-mmm-yyyy HH:MM:SS.FFF'));
0052 if fid<3 && usejava('desktop') && ~strcmp(name(1), '@') && ~strcmp(name, '(none)') % assume stdout/stderr
0053     if showcls
0054         fprintf(fid, '<a href="matlab:opentoline(''%s'', %d, 1)">%s:%d:%s:%s</a>:', ...
0055             which(f), lineno, name, lineno, cl, nm);
0056     else
0057         fprintf(fid, '<a href="matlab:opentoline(''%s'', %d, 1)">%s:%d</a>:', ...
0058             which(f), lineno, name, lineno);
0059     end
0060 elseif haspar % if really par, usejava-desktop is false anyway
0061     t = getCurrentTask();
0062     fprintf(fid, '%s:%d:%d:', name, lineno, t.ID);
0063 else
0064     fprintf(fid, '%s:%d:', name, lineno);
0065 end
0066 fprintf(fid, text, varargin{:});
0067 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005