Skip to content

Commit

Permalink
Some doco and a dist build script
Browse files Browse the repository at this point in the history
  • Loading branch information
apjanke committed Feb 18, 2018
1 parent 90c9ba8 commit e10eb51
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Distribution artifacts
dist/
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This Makefile is just for building the release distribution.
# It's not needed for just building or running the program

.PHONY: dist

PROGRAM=SLF4M
VERSION=$(shell cat VERSION)
DIST=dist/${PROGRAM}-${VERSION}
FILES=README.md LICENSE Mcode doc

dist:
rm -rf dist/*
mkdir -p ${DIST}
cp -R $(FILES) $(DIST)
cd dist; tar czf ${PROGRAM}-${VERSION}.tgz --exclude='*.DS_Store' ${PROGRAM}-${VERSION}
cd dist; zip -rq ${PROGRAM}-${VERSION}.zip ${PROGRAM}-${VERSION} -x '*.DS_Store'
27 changes: 26 additions & 1 deletion Mcode/+logm/Logger.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
% logm.info
% logm.debug
% logm.trace
%
% Examples:
%
% log = logm.Logger.getLogger('foo.bar.FooBar');
% log.info('Hello, world! Running on Matlab %s', version);

properties (SetAccess = private)
% The underlying SLF4J Logger object
Expand All @@ -40,7 +45,6 @@
jLogger = org.slf4j.LoggerFactory.getLogger(identifier);
out = logm.Logger(jLogger);
end

end

methods
Expand All @@ -52,6 +56,27 @@
this.jLogger = jLogger;
end

function disp(this)
disp(dispstr(this));
end

function out = dispstr(this)
if isscalar(this)
strs = dispstrs(this);
out = strs{1};
else
out = sprintf('%s %s', size2str(size(this)), class(this));
end
end

function out = dispstrs(this)
out = cell(size(this));
for i = 1:numel(this)
out{i} = sprintf('Logger: %s (%s)', this(i).name, ...
strjoin(this(i).enabledLevels, ', '));
end
end

function error(this, msg, varargin)
% Log a message at the ERROR level.
if ~this.jLogger.isErrorEnabled()
Expand Down
15 changes: 13 additions & 2 deletions doc/User Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,26 @@ Normally when writing a library, I avoid defining any global functions, to avoid

This provides an extension point for defining custom string conversions for your own user-defined classes. You can override `dispstr` and `dispstrs` in your classes, and SLF4M will recognize it. I find this is useful for other string formatting, too.

For uniformity, if you define `dispstr`, I recommend that you override `disp` to make use of it.
For uniformity, if you define `dispstr`, I recommend that you override `disp` to make use of it. And you'll typically want to make `dispstr` and `dispstrs` consistent.

```
function disp(this)
disp(dispstr(this));
end
% Standard implementation of dispstr
function out = dispstr(this)
if isscalar(this)
strs = dispstrs(this);
out = strs{1};
else
out = sprintf('%s %s', size2str(size(this)), class(this));
end
end
```

As a convenience, there is a `logm.Displayable` mix-in class which provides standard implementations of `disp` and `dispstr` in terms of `dispstrs`. If you inherit from `logm.Displayable`, you only need to define `dispstrs`.
As a convenience, there is a `logm.Displayable` mix-in class which takes care of this boilerplate for you. It provides standard implementations of `disp` and `dispstr` in terms of `dispstrs`. If you inherit from `logm.Displayable`, you only need to define `dispstrs`.

### The `dispstr` interface

Expand Down

0 comments on commit e10eb51

Please sign in to comment.