-
Notifications
You must be signed in to change notification settings - Fork 20
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
[WIP] Define the Chronicle output styles as nim-serialization formats #82
base: master
Are you sure you want to change the base?
Conversation
That's a good start, but please preserve all configurability that controls the use of colors and timestamps. On Windows, I think it's now reasonable for Chronicles to take advantage of the improved support for ANSI escape codes that was shipped 2 years ago: Another expected outcome of the refactoring is that we'll be able to stop using https://github.com/status-im/nim-faststreams/blob/master/faststreams/stdout.nim This stream will reuse a thread-local buffer and as a result most log lines will be generated and sent to the screen without any memory allocations. Targeting |
This latest commit won't work without a commit I made to my I'm not convinced it is generic enough to belong there. I'll have to think about that a bit. If curious, it can be seen at https://github.com/JohnAD/nim-faststreams/blob/master/faststreams/textio.nim#L119 |
You are on the right track, I'm looking forward to see more when-based branching in The next step would be to implement the TextBlocks format as well and to handle the exception types in a special way as the original description in #72 suggests. |
chronicles/textblockserializer.nim
Outdated
w.stream.writeText ": " | ||
|
||
proc writeValue*(w: var TextBlockWriter, value: auto) = | ||
w.stream.writeText value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need code here that will call the right write
routine. For example:
when value is array:
writeIterable(w, value)
elif value is object:
w.stream.write '{'
enumInstanceSerializedFields(value, fieldName, filed):
writeField(w, fieldName, field)
w.stream.write '}'
else ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've now made changes of that nature in both textBlock and textLine.
Since this changes the output of textBlock, I went ahead and enabled growing tabs recursively for objects within objects within object etc.
chronicles/log_output.nim
Outdated
@@ -72,6 +73,13 @@ type | |||
colors: static[ColorScheme]] = object | |||
output*: Output | |||
|
|||
TextBlockBRecord*[Output; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can drop all Record types in favour of a single generic Record
type. It will have a Format
parameter that will be either Json
, TextBlockLog
or TextLineLog
.
From the Format
parameter, you can get the writer type with WriterType(Format)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting a generic Record was fairly straightforward, but a generic Writer was been difficult since I'd want inheritance-like behavior. See question in PR thread.
Do you know of a way in which I can do something like: type
A[x: static[int]] = object of RootObj
blah: string
type
B[x: static[int]] = object of A
morestuff: string
proc say(r: B) =
echo r.morestuff
type
C[x: static[int]] = object of A
otherstuff: string
proc say(r: C) =
echo r.otherstuff
proc bing(foo: A) =
when foo.x == 1:
foo.say
else:
foo.say
foo.say
var testB = B[1](morestuff: "left")
var testC = C[2](otherstuff: "right")
testB.bing() # should print "left" once
testC.bing() # should print "right" twice I've tried various combinations to get a Nim to compile such a thing. I'm not even sure it is possible. Can a class with compile-time generics be inherited from? |
Figured out the answer on myself. Put the answer on the forum just in case others find it useful: |
chronicles/log_output.nim
Outdated
timestamps: static[TimestampsScheme], | ||
colors: static[ColorScheme]] = object | ||
LogRecord*[Output; | ||
logformat: static[LogFormat], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry my explanation wasn't clear enough. The names of the formats such as Json
, TextBlock
and TextLine
are Nim type names. You don't need to (and you shouldn't) pass them as static
parameters.
The correct definition is
type
LogRecord[Output; Format; timestamps: static[TimestampsScheme], ...] = object
output*: Output
writer*: WriterType(Format)
You can initialize the writer
by passing a stream
to its init
function like it's done here:
https://github.com/status-im/nim-json-serialization/blob/master/json_serialization/writer.nim#L20-L26
I haven't tried the above and perhaps the use of WriterType
is pushing Nim a bit too far. If that's the case, you can make the LogRecord
parametric on a Writer
type directly (instead of Format
).
I don't think using inheritance is necessary here. From your example, I cannot see what is the direction you've tried to explore. You might want explain the plan in more detail or you can try to follow my suggestion above, which should get you to a working final design quite soon. |
Attempted the Will try again tomorrow. 😄 |
Was updating the test code to make sure I'm not breaking something fundamental yet. And... Support for In fact, you might see a new PR request if it is too radically different. |
It suffers from a flaw that the serialization format approach will solve. Log statements from third party libraries won't use your |
Just FYI, I'll be able to get back on this project on June 4th. |
ref: #72
Ignore this first commit as I'm just playing around with the existing setup. I created an example chronicle stream similar to
textLines
.I will be making a Writer next.
@zah I noticed that you have already taken the changes in the
latest-fs
branch and merged them intomaster
. Should I redo this PR againstlatest-fs
or stay here?