Skip to content

Commit be21b0b

Browse files
committed
Add mp_warning() and use it in place of warning() to allow redirecting console output.
1 parent fd6c1cb commit be21b0b

16 files changed

Lines changed: 239 additions & 33 deletions

CHANGES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ Change history for MP-Test
55
since 8.1
66
---------
77

8+
#### 8/4/26
9+
- Use recently added `mp_warning()` in place of `warning()` everywhere to
10+
allow redirecting of console output.
11+
- Add `mp_warning()` function which can be used as a drop-in replacement for
12+
`warning()` for throwing warnings, but not for querying/controling warning
13+
status. By default it behaves identically, but can use an `mp.logger` object
14+
to redirect the output to a file, or elsewhere via a custom `mp.logger`
15+
subclass.
16+
817
#### 7/30/26
918
- Use recently added `mp_printf()` in place of `fprintf()` everywhere to
1019
allow redirecting of console output.

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ of MATLAB or Octave, including setting up your MATLAB/Octave path.
3737
t_debug_assert....ok
3838
t_have_feature....ok
3939
t_mp_logger.......ok
40-
All tests successful (51 of 51)
41-
Elapsed time 0.20 seconds.
40+
All tests successful (96 of 96)
41+
Elapsed time 0.23 seconds.
4242
```
4343

4444
Usage
@@ -290,6 +290,17 @@ particular function.
290290
object, or elsewhere via a custom `mp.logger` subclass. If the first
291291
argument is a file ID, it does not redirect anything.
292292

293+
- __mp_warning__ — drop-in replacement for `warning()`
294+
```
295+
mp_warning(msg)
296+
mp_warning(format_str, arg1, ...)
297+
```
298+
Optionally redirects the output of `warning()` to a file via an `mp.logger`
299+
object, or elsewhere via a custom `mp.logger` subclass. Inputs are identical
300+
to those of `warning()`. Note that this function is only for throwing a
301+
warning, not querying or controlling warning status, and the on/off state
302+
of a warning has no effect on redirected output.
303+
293304
- __mp.logger_manager__ — manage redirection for `mp_disp()` and `mp_printf()`
294305
```
295306
mp.logger.manager('init', 'path/to/my/log-file.txt')
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. currentmodule:: mptest
2+
3+
:raw-html:`<div style="float: right"><a href="https://github.com/MATPOWER/mptest/blob/master/lib/mp_warning.m" target=_blank><svg height="32" aria-hidden="true" viewBox="0 0 16 16" version="1.1" width="32" data-view-component="true" class="octicon octicon-mark-github v-align-middle color-fg-default"><path d="M8 0c4.42 0 8 3.58 8 8a8.013 8.013 0 0 1-5.45 7.59c-.4.08-.55-.17-.55-.38 0-.27.01-1.13.01-2.2 0-.75-.25-1.23-.54-1.48 1.78-.2 3.65-.88 3.65-3.95 0-.88-.31-1.59-.82-2.15.08-.2.36-1.02-.08-2.12 0 0-.67-.22-2.2.82-.64-.18-1.32-.27-2-.27-.68 0-1.36.09-2 .27-1.53-1.03-2.2-.82-2.2-.82-.44 1.1-.16 1.92-.08 2.12-.51.56-.82 1.28-.82 2.15 0 3.06 1.86 3.75 3.64 3.95-.23.2-.44.55-.51 1.07-.46.21-1.61.55-2.33-.66-.15-.24-.6-.83-1.23-.82-.67.01-.27.38.01.53.34.19.73.9.82 1.13.16.45.68 1.31 2.69.94 0 .67.01 1.3.01 1.49 0 .21-.15.45-.55.38A7.995 7.995 0 0 1 0 8c0-4.42 3.58-8 8-8Z"></path></svg></a></div>`
4+
5+
mp_warning
6+
----------
7+
8+
.. autofunction:: mp_warning
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../lib/mp_warning.m

docs/sphinx/source/reference.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ optional functionality and to check the version of the installed MP-Test.
3434
functions/mptestver
3535
functions/mp_disp
3636
functions/mp_printf
37+
functions/mp_warning
3738
classes/mp/logger
3839
functions/toggle_debug_mode
3940

lib/+mp/logger.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
% * init - initialize logger object (open log file)
2020
% * set_file - open log file, after closing any already open
2121
% * printf - prints to log
22+
% * warning - warnings to log
2223
% * manage - handle actions forwarded from mp.logger.manager
2324
% * finalize - finalize logger object (close log file)
2425
% * manager - manage the logger object used by mp_printf and mp_disp
@@ -167,6 +168,52 @@
167168
end
168169
end
169170

171+
function obj = warning(obj, varargin)
172+
% Warnings to log.
173+
% ::
174+
%
175+
% obj.warning(...)
176+
%
177+
% Inputs are identical to those of ``warning()``. Note, this
178+
% function is only for throwing a warning, not querying or
179+
% controlling warning status.
180+
181+
if nargin < 2
182+
error('mp.logger.warning: no warning message');
183+
else
184+
if obj.fid > 0 %% warn to log file
185+
msg = sprintf(varargin{:});
186+
lastwarn(msg);
187+
st = dbstack('-completenames');
188+
if have_feature('matlab')
189+
fprintf(obj.fid, 'Warning: %s\n', msg);
190+
if length(st) > 0
191+
fprintf(obj.fid, '> ');
192+
end
193+
for k = 2:length(st)
194+
fprintf(obj.fid, 'In %s (line %d)\n', ...
195+
st(k).name, st(k).line);
196+
end
197+
else
198+
fprintf(obj.fid, 'warning: %s\n', msg);
199+
if length(st) > 0
200+
fprintf(obj.fid, 'warning: called from\n');
201+
end
202+
for k = 2:length(st)
203+
fprintf(obj.fid, ' %s at line %d column %d\n', ...
204+
st(k).name, st(k).line, st(k).column);
205+
end
206+
fprintf(obj.fid, '\n');
207+
end
208+
if obj.manual_flush
209+
fflush(obj.fid);
210+
end
211+
else
212+
error('mp.logger.warning: log file not open');
213+
end
214+
end
215+
end
216+
170217
function varargout = manage(obj, action, varargin)
171218
% Handle actions forwarded from mp.logger.manager.
172219
% ::

lib/have_feature.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
% have_feature(cache, 'set_cache')
111111

112112
% MP-Test
113-
% Copyright (c) 2004-2024, Power Systems Engineering Research Center (PSERC)
113+
% Copyright (c) 2004-2026, Power Systems Engineering Research Center (PSERC)
114114
% by Ray Zimmerman, PSERC Cornell
115115
%
116116
% This file is part of MP-Test.
@@ -174,7 +174,7 @@
174174
%% check for feature
175175
fcn = ['have_feature_' tag];
176176
if isempty(which(fcn))
177-
warning('have_feature: unknown functionality ''%s''', tag);
177+
mp_warning('have_feature: unknown functionality ''%s''', tag);
178178
vstr = 'unknown';
179179
else
180180
[TorF, vstr, rdate] = feval(fcn);

lib/have_feature_matlab.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
% See also have_feature.
88

99
% MP-Test
10-
% Copyright (c) 2004-2024, Power Systems Engineering Research Center (PSERC)
10+
% Copyright (c) 2004-2026, Power Systems Engineering Research Center (PSERC)
1111
% by Ray Zimmerman, PSERC Cornell
1212
%
1313
% This file is part of MP-Test.
@@ -16,7 +16,7 @@
1616

1717
v = ver('matlab');
1818
if length(v) > 1
19-
warning('The built-in VER command is behaving strangely, probably as a result of installing a 3rd party toolbox in a directory named ''matlab'' on your path. Check each element of the output of ver(''matlab'') to find the offending toolbox, then move the toolbox to a more appropriately named directory.');
19+
mp_warning('The built-in VER command is behaving strangely, probably as a result of installing a 3rd party toolbox in a directory named ''matlab'' on your path. Check each element of the output of ver(''matlab'') to find the offending toolbox, then move the toolbox to a more appropriately named directory.');
2020
v = v(1);
2121
end
2222
if ~isempty(v) && isfield(v, 'Version') && ~isempty(v.Version)

lib/mp_disp.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ function mp_disp(obj)
66
% mp_disp(something_to_be_displayed);
77
% mp.logger.manager('clear');
88
%
9+
% Optionally redirects the output of ``disp()`` to a file via an mp.logger
10+
% object, or elsewhere via a custom mp.logger subclass.
11+
%
912
% See also mp.logger.manager, mp.logger.
1013

1114
% MP-Test

lib/mp_printf.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ function mp_printf(varargin)
66
% mp_printf('A line of %s to be printed.\n', 'text');
77
% mp.logger.manager('clear');
88
%
9+
% Optionally redirects the output of ``fprintf()`` to a file via an mp.logger
10+
% object, or elsewhere via a custom mp.logger subclass. If the first argument
11+
% is a file ID, it does not redirect anything.
12+
%
913
% See also mp.logger.manager, mp.logger.
1014

1115
% MP-Test

0 commit comments

Comments
 (0)