LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
line_warning.m
1function line_warning(caller, MSG, varargin)
2% LINE_WARNING(CALLER, ERRMSG)
3
4% Copyright (c) 2012-2026, Imperial College London
5% All rights reserved.
6
7if ~coder.target('MATLAB')
8 return; % No-op in codegen mode
9end
10
11%global GlobalConstants.Verbose
12persistent lastWarning;
13persistent suppressedWarnings;
14persistent suppressedWarningsTic;
15persistent lastWarningTime;
16persistent suppressedAnnouncement;
17
18if GlobalConstants.Verbose == VerboseLevel.SILENT
19 return
20end
21
22suppressedAnnouncement = false;
23errmsg=sprintf(MSG, varargin{:});
24w = warning('QUERY','ALL');
25w(1).state = 'on'; % always print warnings by default
26switch w(1).state
27 case 'on'
28 %warning('[%s] %s',caller,MSG);
29 finalmsg = sprintf('Warning [%s.m]: %s',caller,errmsg);
30 % Terminate the warning with a newline, as line_warning_always and the
31 % Python/JAR loggers do. line_printf writes the string verbatim, so a
32 % call site that does not embed a trailing '\n' leaves the next
33 % printout glued to the warning (e.g. 'reference tasks only.AvgTable =').
34 if isempty(finalmsg) || finalmsg(end) ~= sprintf('\n')
35 finalmsg = [finalmsg, sprintf('\n')];
36 end
37 try
38 % Time since suppression began. The previous form,
39 % toc(suppressedWarningsTic)-toc(lastWarningTime), cancels the
40 % current time and evaluates to lastWarningTime-suppressedWarningsTic,
41 % a fixed gap between two past instants that is ~0 because both are
42 % set in the same call. It therefore never exceeded 60 and an
43 % identical message stayed suppressed for the whole session rather
44 % than for a minute, diverging from the JAR and Python
45 % implementations which both measure elapsed time.
46 if ~strcmp(finalmsg, lastWarning) || toc(suppressedWarningsTic)>60
47 line_printf(finalmsg);
48 %warning(finalmsg);
49 lastWarning = finalmsg;
50 suppressedWarnings = false;
51 suppressedWarningsTic = tic;
52 else
53 if ~suppressedWarnings && ~suppressedAnnouncement
54 %line_printf(finalmsg);
55 %warning(finalmsg);
56 finalmsg = sprintf('\nWarning [%s.m]: %s',caller,errmsg);
57 line_printf(sprintf('[%s.m] %s',caller,'Message casted more than once, repetitions will not be printed on screen for 60 seconds.\n'));
58 %warning(sprintf('[%s.m] %s',caller,'Message casted more than once, repetitions will not be printed on screen for 60 seconds.'));
59 suppressedAnnouncement = true;
60 suppressedWarnings = true;
61 suppressedWarningsTic = tic;
62 end
63 end
64 lastWarningTime=tic;
65 catch ME
66 switch ME.identifier
67 case 'MATLAB:toc:callTicFirstNoInputs'
68 %warning(finalmsg);
69 line_printf(finalmsg);
70 lastWarning = finalmsg;
71 suppressedWarnings = false;
72 suppressedWarningsTic = -1;
73 lastWarningTime=tic;
74 end
75 end
76 case 'off'
77 %line_printf(sprintf('Warning [%s.m]: %s',caller,errmsg));
78end
79end