Skip to content

glog error log was incorrrectly written to unexpected memory #561

Closed
@fanyhawu

Description

Hi,
I know glog ERROR log message will be written to stderr by default. Most of the time, it work as expected, but occasionally I found it will not be written into stderr and was written into some unexpected memory.
I have checked my source code and then I found there's something that I may did incorrectly:

class NullLogger : public google::base::Logger
{
	virtual void Write(bool, time_t, const char*, int) {
		
	}
	virtual void Flush() {
		
	}
	virtual google::uint32 LogSize() {
		return 0;
	}
};

void init()
{
      ...
      google::SetLogDestination(google::GLOG_INFO, fullpath.c_str());

      // do not generate separate files for warning and error.
	static NullLogger nullLogger;
	google::base::SetLogger(google::GLOG_WARNING,&nullLogger);
	google::base::SetLogger(google::GLOG_ERROR,&nullLogger);
       google::InitGoogleLogging("");
}

note I called google::base::SetLogger() before google::InitGoogleLogging().
If I called google::InitGoogleLogging() first, then call google::base::SetLogger(), just like following code, everything seems work fine, but I can't explain why.

void init()
{
      ...
      google::SetLogDestination(google::GLOG_INFO, fullpath.c_str());
      google::InitGoogleLogging("");
      // do not generate separate files for warning and error.
	static NullLogger nullLogger;
	google::base::SetLogger(google::GLOG_WARNING,&nullLogger);
	google::base::SetLogger(google::GLOG_ERROR,&nullLogger);
       
}

I know for glog some operations should be done before google::InitGoogleLogging(), such as "setting value of FLAGS_log_dir".
I have checked google::InitGoogleLogging() source code, it looks it only defines the g_main_thread_id and marked if it has been initialized. my question is: if I call google::base::SetLogger() before google::InitGoogleLogging(), will there be some issue that you can imagine?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions