TensorFlow.NET icon indicating copy to clipboard operation
TensorFlow.NET copied to clipboard

Replace Console.WriteLine with MessageHandler

Open nick2893 opened this issue 6 years ago • 9 comments

I've found several places with the code such as:

Console.WriteLine($"Restoring parameters from {save_path}");

This becomes a bit troublesome if I'm using TensorFlow.NET in a Windows Service.

May I suggest something like this instead?

    public void DefaultMessageHandler(string sMessage)
    {
        Console.WriteLine(sMessage);
    }

public delegate void OneStringArgDelegate(string sWhat); public OneStringDelegate NonDefaultMessageHandler;

    public void MessageHandler(string sMessage)
    {
        if (NonDefaultMessageHandler == null)
            DefaultMessageHandler(sMessage);
        else
            NonDefaultMessageHandler(sMessage);
    }

nick2893 avatar Dec 13 '19 17:12 nick2893

@nick2893 Thanks for pointing out this issue. I'm not sure about it, does Windows Service has issue to handle console output? can you paste the error message?

Oceania2018 avatar Dec 13 '19 17:12 Oceania2018

We're not that far along yet, but Windows Service can be installed to run with a different windows login name, and to be unable to interact with the local desktop session. It's no trouble to write any messages to the Windows Event Log instead, as long as we have a way to do it. We will also have an application with a GUI, so in that scenario, a console window popping up will confuse the end user.

nick2893 avatar Dec 13 '19 17:12 nick2893

Should we inject logger somewhere?

kerryjiang avatar Dec 13 '19 17:12 kerryjiang

@kerryjiang That's a good idea. Logger should be added to TF.NET instead of Console.Write. @nick2893 Your concern make sense. ILogger is a better solution.

Oceania2018 avatar Dec 13 '19 18:12 Oceania2018

This is another message that I saw; it was notable to me because it seems to originate from tensorflow.dll

2019-12-13 12:34:‪19.988143‬: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

nick2893 avatar Dec 13 '19 18:12 nick2893

@nick2893 You have to compile your own tensorflow binary to remove the message.

Oceania2018 avatar Dec 13 '19 18:12 Oceania2018

@Oceania2018 was that message written to the console by tensorflow.dll?

kerryjiang avatar Dec 13 '19 18:12 kerryjiang

This one is YES. 2019-12-13 12:34:‪19.988143‬: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

Oceania2018 avatar Dec 13 '19 19:12 Oceania2018

@Oceania2018 does TensorFlow.net support some way to write this TensorFlow output to a file? Something like this is used in Python:

import logging

// get TF logger log = logging.getLogger('tensorflow') log.setLevel(logging.DEBUG)

// create formatter and add it to the handlers formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

// create file handler which logs even debug messages fh = logging.FileHandler('tensorflow.log') fh.setLevel(logging.DEBUG) fh.setFormatter(formatter) log.addHandler(fh)

Inzta avatar May 07 '21 07:05 Inzta