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

How does bazel use WITH_CUSTOM_PREFIX flag? #675

Closed
luliyucoordinate opened this issue Jun 27, 2021 · 5 comments · Fixed by #676
Closed

How does bazel use WITH_CUSTOM_PREFIX flag? #675

luliyucoordinate opened this issue Jun 27, 2021 · 5 comments · Fixed by #676

Comments

@luliyucoordinate
Copy link
Contributor

I want to change log prefix format. And I see the sentence "That feature must be enabled at compile time by the WITH_CUSTOM_PREFIX flag." in the document. But how do I use it in bazel?

@sergiud
Copy link
Collaborator

sergiud commented Jun 27, 2021

WITH_CUSTOM_PREFIX is a CMake option. In Bazel, you need to define GLOG_CUSTOM_PREFIX_SUPPORT.

@sergiud sergiud closed this as completed Jun 27, 2021
@luliyucoordinate
Copy link
Contributor Author

When I defined GLOG_CUSTOM_PREFIX_SUPPORT at the beginning

#define GLOG_CUSTOM_PREFIX_SUPPORT
#include <iostream>
#include <iomanip>
#include <glog/logging.h>

using std::setw;
using std::setfill;


/* This function writes a prefix that matches glog's default format.
 * (The third parameter can be used to receive user-supplied data, and is
 * NULL by default.)
 */
void CustomPrefix(std::ostream &s, const LogMessageInfo &l, void*) {
   s << l.severity[0]
   << setw(4) << 1900 + l.time.year()
   << setw(2) << 1 + l.time.month()
   << setw(2) << l.time.day()
   << ' '
   << setw(2) << l.time.hour() << ':'
   << setw(2) << l.time.min()  << ':'
   << setw(2) << l.time.sec() << "."
   << setw(6) << l.time.usec()
   << ' '
   << setfill(' ') << setw(5)
   << l.thread_id << setfill('0')
   << ' '
   << l.filename << ':' << l.line_number << "]";
}

int main(int argc, char* argv[]) {
    // FLAGS_log_dir  = "./log";
    google::InitGoogleLogging(argv[0], &CustomPrefix);
    FLAGS_logtostderr = 1;
    int num_cookies = 10;
    LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";

    for (int i = 0; i < 30; i++) {
        LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
    }

    DLOG(INFO) << "Found cookies";
    // CHECK_EQ(1, 2) << ": The world must be ending!";

    FLAGS_v = 2;
    VLOG(1) << "I’m printed when you run the program with --v=1 or higher";
    VLOG(2) << "I’m printed when you run the program with --v=2 or higher";

    google::ShutdownGoogleLogging();
}

and l get error

bazel-out/k8-fastbuild/bin/_objs/main/main.pic.o:main.cc:function main: error: undefined reference to 'google::InitGoogleLogging(char const*, void (*)(std::ostream&, LogMessageInfo const&, void*), void*)'
collect2: error: ld returned 1 exit status
Target //:main failed to build

@sergiud sergiud reopened this Jun 28, 2021
@sergiud
Copy link
Collaborator

sergiud commented Jun 28, 2021

/cc @drigz

@drigz
Copy link
Member

drigz commented Jun 28, 2021

@luliyucoordinate The current Bazel integration doesn't seem to support it. You could try running bazel build --copt=-DGLOG_CUSTOM_PREFIX_SUPPORT .... If that helps, you could try adding it to common_copts in glog.bzl and sending a PR.

@luliyucoordinate
Copy link
Contributor Author

@drigz it works, then l sending a PR bazel: add GLOG_CUSTOM_PREFIX_SUPPORT

@sergiud sergiud linked a pull request Jun 30, 2021 that will close this issue
@drigz drigz closed this as completed in #676 Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants