Skip to content

Conversation

mamaso
Copy link
Contributor

@mamaso mamaso commented Dec 16, 2016

resolves #304

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm finding the following code very difficult to understand/read. Maybe its just me, but its making my head hurt

Copy link
Contributor Author

@mamaso mamaso Dec 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair, a more C# style is probably better for maintainability

basic idea was 'log' is an object of tracewriter methods. We need to decorate those methods with the util.format code and then get it into a format where context.log is the info tracewriter with all the tracewriter methods attached to it as properties (C# doesn't seems to have the idea of callable objects (I guess it would be extending Func?), which is what this is mapping to)

Copy link
Member

@mathewc mathewc Dec 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that you could construct the set of log methods in this code, but have them all funnel down into a single log call into the func exposed from .NET - just passing in an object of the form { level: 'info', message: '...' }. That allows us to just have a single simple method in .NET receiving all of these. For example:

var origLog = context.log;
var logLevel = function (level) {
    return function () {
        var message = util.format.apply(null, args);
        origLog({ level: level, message: message});
    };
}
var levels = ['info', 'verbose', 'warn', 'error' ];
var log = logLevel('info');
levels.foreach(level => {
    log[level] = logLevel(level);
});
context.log = log;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking this would all be much simpler if we just make the existing log be a function that takes and object like { level: 'info', message: '...' }. Our node adapter code then passes in such objects, and we map like:

// create a TraceWriter wrapper that can be exposed to Node.js
var log = (Func<object, Task<object>>)(p =>
{
    var logData = (IDictionary<string, object>)p;
    string message = (string)logData["message"];
    if (message != null)
    {
        try
        {
            TraceLevel level = (TraceLevel)Enum.Parse(typeof(TraceLevel), (string)logData["level"]);
            var evt = new TraceEvent(level, message);
            traceWriter.Trace(evt);
        }
        catch (ObjectDisposedException)
        {
            // if a function attempts to write to a disposed
            // TraceWriter. Might happen if a function tries to
            // log after calling done()
        }
    }

    return Task.FromResult<object>(null);
});

Copy link
Member

@mathewc mathewc Dec 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy paste error? Anyhow, I think you should just add your new cases to the existing logging scenario script and test. simpler that way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with my suggestion above, this change shouldn't be needed, right? since log remains a function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right

@mamaso mamaso force-pushed the loglevl branch 2 times, most recently from 30ecf17 to e303db4 Compare December 20, 2016 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider adding context.trace (or context.verbose)
4 participants