Last active
February 26, 2018 23:36
-
-
Save waldyrious/c47b9b397d5956ae5a9d24b386047d71 to your computer and use it in GitHub Desktop.
Matlab/Octave function to produce a formatted log entry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Return a formatted string that can be added to a log. | |
% Available log levels and recommended use cases: | |
% -- TRACE: Leave breadcrumbs of the program flow; provide context. | |
% Printed in blue. | |
% -- DEBUG: Expose internal program state, for development or troubleshooting. | |
% Printed in purple | |
% -- INFOR: Notify user of normal/expected actions and events. | |
% Printed in green | |
% -- WARNG: Alert of incorrect/unexpected input; automatically recoverable. | |
% Printed in yellow. | |
% -- ERROR: Issues fatal to the current operation, but not the entire program. | |
% Printed in orange. | |
% -- FATAL: Issues that force immediate halting the program to avoid data loss. | |
% Printed in white on red. | |
% Note: the ESC character must be inputted as `char(27)`: | |
% Ref: http://octave.1599824.n4.nabble.com/Color-console-is-this-possible-in-near-future-tp4646309p4646312.html | |
% TODO: add timestamp. | |
% TODO: detect if ANSI escape codes are supported | |
% TODO: wrapper script to print out directly as if fprintf(log_entry(...)) | |
% TODO: should also allow calling with extra parameters for printf-like params. | |
function log_str = log_entry(message, level) | |
levels = {'trace', 'debug', 'infor', 'warng', 'error', 'fatal'}; | |
colors = {'34', '35', '32', '33', '31', '37;41'}; | |
log_str = ''; % Default return value | |
% Process the "message" parameter | |
if ~exist('message', 'var') || isempty(message) | |
fprintf(cstrcat('[ERROR] No message provided. ' ... | |
, 'Usage: log_entry(message, [level])\n' ... | |
)); | |
return; | |
elseif ~strcmp(class(message), 'char') | |
fprintf(cstrcat('[ERROR] log_entry(): Unsupported message type.\n\t' ... | |
, 'Expected a string ("char" class), ' ... | |
, 'got "', class(message), '" class instead.\n' ... | |
)); | |
return; | |
fprintf('this should not show up.\n'); | |
end%if | |
% Process the "level" parameter | |
if ~exist('level', 'var') || isempty(level) | |
level = 'infor'; | |
elseif ~strcmp(class(level), 'char') | |
fprintf(cstrcat('[WARNG] log_entry(): Unsupported log level type.\n\t' ... | |
, 'Expected a string ("char" class), ' ... | |
, 'got "', class(level), '" class instead.\n' ... | |
)); | |
fprintf('[INFOR] log_entry(): Defaulting to "infor" level.\n'); | |
level = 'infor'; | |
elseif strcmp(level, 'info') | |
level = 'infor'; | |
elseif strcmp(level, 'warn') | |
level = 'warng'; | |
elseif ~any(ismember(levels, level)) | |
fprintf(cstrcat('[WARNG] log_entry(): Unsupported log level name.\n\t' ... | |
, 'Expected one of: ', strjoin(levels,', '), '; ' ... | |
, 'got "', level, '" instead.\n' ... | |
)); | |
fprintf('[INFOR] log_entry(): Defaulting to "infor" level.\n'); | |
level = 'infor'; | |
end%if | |
% Get color at the same index as the log level | |
color = colors{find(ismember(levels, level))}; | |
% Generate the formatted log entry string | |
log_str = [ ... | |
char(27) '[1;' color 'm' '[' upper(level) ']' char(27) '[0m ' ... | |
message '\n' ... | |
]; | |
end%function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment