Skip to content

buffer messages before writing to file #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion +logging/logging.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@
datefmt_ = 'yyyy-mm-dd HH:MM:SS,FFF';
logLevel_ = logging.logging.INFO;
commandWindowLevel_ = logging.logging.INFO;
bufferedMessages_ = cell(1);
bufferCursor_ = 1;
end

properties (Dependent)
datefmt;
logLevel;
commandWindowLevel;
bufferingSize;
end

methods(Static)
Expand Down Expand Up @@ -94,6 +97,10 @@ function setCommandWindowLevel(self, level)
function setLogLevel(self, level)
self.logLevel = level;
end

function setBufferingSize(self, bufferSize)
self.bufferingSize = bufferSize;
end

function tf = ignoreLogging(self)
tf = self.commandWindowLevel_ == self.OFF && self.logLevel_ == self.OFF;
Expand Down Expand Up @@ -142,12 +149,14 @@ function critical(self, message)
p.addParameter('logLevel', self.logLevel);
p.addParameter('commandWindowLevel', self.commandWindowLevel);
p.addParameter('datefmt', self.datefmt_);
p.addParameter('bufferingSize', 1);
p.parse(name, varargin{:});
r = p.Results;

self.name = r.name;
self.commandWindowLevel = r.commandWindowLevel;
self.datefmt = r.datefmt;
self.setBufferingSize(r.bufferingSize);
if ~isempty(r.path)
self.setFilename(r.path); % Opens the log file.
self.logLevel = r.logLevel;
Expand Down Expand Up @@ -184,7 +193,13 @@ function writeLog(self, level, caller, message)
end

if self.logLevel_ <= level && self.logfid > -1
fprintf(self.logfid, logline);
self.bufferedMessages_{self.bufferCursor_} = logline;
if self.bufferCursor_ == numel(self.bufferedMessages_)
cellfun(@(x) fprintf(self.logfid, x), self.bufferedMessages_);
self.bufferCursor_ = 1;
else
self.bufferCursor_ = self.bufferCursor_ + 1;
end
end
end

Expand Down Expand Up @@ -220,6 +235,20 @@ function writeLog(self, level, caller, message)
function level = get.commandWindowLevel(self)
level = self.commandWindowLevel_;
end

function set.bufferingSize(self, bufferSize)
validateattributes(bufferSize, {'numeric'}, {'scalar', 'integer', 'nonzero', 'positive'});
n = numel(self.bufferedMessages_);
if bufferSize > n
self.bufferedMessages_ = [self.bufferedMessages_ cell(1, bufferSize - n)];
else
self.bufferedMessages_ = self.bufferedMessages_(1:bufferSize);
end
end

function bufferSize = get.bufferingSize(self)
bufferSize = numel(self.bufferedMessages_);
end


end
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ The available arguments are:
This contains the date/time format string used by the logs.
The format must be compatible with the built-in `datestr` function.
This must be a string.
* `bufferingSize` : set the number buffered logging lines berfore writing to file.
Must be a positive integer.

If `logger` is an instance of the `logging` class, the following methods can be used
to log output at different levels:
Expand Down