Skip to content
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

When log a char buff[size] veriable which create on stack, it will may log a invalid variable in write thread. #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

edigar007
Copy link

string_literal_t's constructor put the point of arg to m_cs, not a deep copy. when the arg is a veriable create on stack , or has ben changed, it will may log a invalid variable.

…y log a invalid variable in write thread.

string_literal_t's constructor put the point of arg to m_cs, not a deep copy. when the arg is a veriable create on stack , or has ben changed, it will may log a invalid variable.
When log a char buff[size] veriable which create on stack, it will may log a invalid variable in write thread.
@edwardlucas
Copy link

This fix works for me.

Testing code below

#include <iostream>
#include <cstring>
#include "NanoLog.hpp"

extern const std::string VERSION = "this is a global std::string";

int main(int, char**) {
    nanolog::initialize(nanolog::GuaranteedLogger(), "log/", "nanologtest", 128);

    char s[64];
    strncpy(s, "this is a char", sizeof("this is a char"));
    LOG_INFO << "log a local char[]";
    LOG_INFO << s;
    LOG_INFO << "Log global string: '" << VERSION << "'";
}

result before fix (local char string always absent):

faund@bigwhite:~/Develop/tradingprojects/NanoLog/build/log$ more nanologtest.1.txt 
[2021-04-02 07:59:14.883606][INFO][139943388731200][/home/faund/Develop/tradingprojects/NanoLog/main.cpp:main:12] log a local char[]
[2021-04-02 07:59:14.883614][INFO][139943388731200][/home/faund/Develop/tradingprojects/NanoLog/main.cpp:main:13] 
[2021-04-02 07:59:14.883618][INFO][139943388731200][/home/faund/Develop/tradingprojects/NanoLog/main.cpp:main:14] Log global string: 'this is a global std::string'

result after fix (local char string logged):

faund@bigwhite:~/Develop/tradingprojects/NanoLog/build/log$ more nanologtest.1.txt 
[2021-04-02 08:03:26.105471][INFO][139948852975424][/home/faund/Develop/tradingprojects/NanoLog/main.cpp:main:12] log a local char[]
[2021-04-02 08:03:26.105481][INFO][139948852975424][/home/faund/Develop/tradingprojects/NanoLog/main.cpp:main:13] this is a char
[2021-04-02 08:03:26.105483][INFO][139948852975424][/home/faund/Develop/tradingprojects/NanoLog/main.cpp:main:14] Log global string: 'this is a global std::string'

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.

2 participants