% MSG writes a message to the MATLAB terminal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DESCRIPTION: Writes a message to the MATLAB terminal if its priority is
% equal or higher than the verbosity level in the LTPDA user preferences. The
% message can be a printf format string that describes how subsequent argument
% sare converted for output.
%
% CALL:     utils.helper.msg(priority, message, ...)
%
% INPUTS:
%           message  - message string to print
%           priority - priority of the message as defined in utils.const.msg
%
% EXAMPLE:
%
%     >> import utils.const.*
%     >> utils.helper.msg(msg.IMPORTANT, 'The Answer is %d.', 42);
%
% NOTE: The verbosity level can be set within the 'LTPDAprefs' dialog, or via
%
%     >> p = getappdata(0, 'LTPDApreferences');
%     >> p.display.verboseLevel
%     >> p.display.verboseLevel = utils.const.msg.PROC1
%
% VERSION:  $Id: msg.m,v 1.15 2010/08/16 14:04:58 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function msg(varargin)
  level = varargin{1};
  
  % get prefereces
  persistent prefs;
  prefs = getappdata(0, 'LTPDApreferences');
  
  % decide to print or not
  if ~isempty(prefs) && level <= double(prefs.getDisplayPrefs.getDisplayVerboseLevel)
    
    % format message
    msg = sprintf(varargin{2}, varargin{3:end});
    msg = ['M: ' repmat(char(32), 1, 2*level) msg];
    
    fprintf('%s\n', msg)
    wb = getappdata(0, 'LTPDAworkbench');
    if isa(wb, 'LTPDAworkbench')
      wb.addMessage(msg);
    end
  end
end

