Skip to content

Instantly share code, notes, and snippets.

@danmackinlay
Created January 30, 2012 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danmackinlay/1707216 to your computer and use it in GitHub Desktop.
Save danmackinlay/1707216 to your computer and use it in GitHub Desktop.
/*
* file-based loggers, because logging to a GUI window is no good when SuperCollide does its segfault thing.
*/
NullLogger {
/* this parent class provdes a black hole logger so that you can stop
logging without changing code. */
formatMsg {|msgs|
var stampedMsgs = msgs;
//A nil in the first msg argument will be replaced by a datestamp
msgs[0].isNil.if({
stampedMsgs = msgs.copy;
stampedMsgs[0] = Date.gmtime.stamp;
});
^"|||"+stampedMsgs.join("|")++"\n";
}
*new {|fileName|
^super.new;
}
*newFromDate {|prefix|
^this.new;
}
*global {
^this.new;
}
*default {
^this.new;
}
log {|...msgargs|
^this.formatMsg(msgargs);
}
logFlush {|...msgargs|
^this.log(*msgargs);
}
}
LogFile : NullLogger {
/* writes pipe-separated log messages */
classvar global;
classvar default;
classvar <>logpath = "~/Logs/Supercollider";
var file;
var <fileName;
*new {|fileName|
var file, thisLogPath;
thisLogPath = PathName(logpath);
fileName.isNil.if({
"No log name supplied".throw;
});
File.exists(thisLogPath.fullPath).not.if({
File.mkdir(thisLogPath.fullPath);
});
fileName = (thisLogPath +/+ fileName).fullPath;
file = File.open(fileName, "a");
^super.newCopyArgs(file, fileName);
}
*newFromDate {|prefix|
var fileName;
fileName = Date.gmtime.stamp;
prefix.notNil.if({
fileName = prefix ++ "-" ++ fileName ++ ".log";
});
^this.new(fileName);
}
*global {
/* a shared, appendable log that all local supercolldier procs
can write to. */
global.isNil.if({global = this.new("_global")});
^global;
}
*default {
/* a fresh, time-stamped logfile for your ease of logging */
default.isNil.if({default = this.newFromDate});
^default;
}
log {|...msgargs|
var formatted = this.formatMsg(msgargs);
file.write(formatted);
^formatted;
}
logFlush {|...msgargs|
var formatted = this.log(*msgargs);
file.flush;
^formatted;
}
}
(
~log = LogFile.global;
~iterations = 0;
~list = LinkedList.new;
~term = {|list, iterations|
var aliens;
~log.logFlush("term").postln;
aliens = list.detect({|i, idx| i.class!=Event;} );
aliens.notNil || iterations>10; };
~birth = { (\a: 5.rand, \b: 10.rand, \birthday: ~iterations);};
~tend = {|list|
var doomed, deathCount;
~log.logFlush("tend").postln;
doomed = list.select({0.1.coin;});
~log.logFlush("doomed", doomed).postln;
deathCount = doomed.size;
doomed.do({|i| list.remove(i);});
deathCount.do({~list.add(~birth.value)});
~iterations = ~iterations +1;
};
~rout = Routine.new({
while(
{
~log.logFlush("whiletest", ~iterations).postln;
(~term.value(
~list, ~iterations
).not)
},
{
~log.logFlush("whiledo").postln;
~tend.value(~list);
true.yield;
};
);
false.yield;},
stackSize: 1024
);
100.do({~list.add(~birth.value);});
while {~rout.next } {
//noop
};
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment