-
Notifications
You must be signed in to change notification settings - Fork 222
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
add a logging alternative #80
Changes from all commits
3839c3a
84e93ce
b291ed3
154364e
9ab00fb
07a7774
882f44b
ead061a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR) | |
project(k2) | ||
|
||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) | ||
set(CMAKE_CXX_STANDARD 11) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) # require c++ standard >= 11 | ||
|
||
option(BUILD_SHARED_LIBS "Whether to build shared or static lib" ON) | ||
|
||
|
@@ -33,11 +35,16 @@ if(WIN32 AND BUILD_SHARED_LIBS) | |
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE) | ||
endif() | ||
|
||
# build options | ||
option(K2_USE_GLOG "Flag that chooses logger from [GLOG, NON-GLOG (loguru)]" OFF) | ||
|
||
enable_testing() | ||
|
||
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) | ||
include(cpplint) | ||
include(glog) | ||
if(K2_USE_GLOG) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if(K2_USE_GLOG)
include(glog)
endif() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed. |
||
include(glog) | ||
endif() | ||
include(googletest) | ||
include(pybind11) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,9 @@ | |
#include <utility> | ||
#include <vector> | ||
|
||
#include "glog/logging.h" | ||
#include "k2/csrc/array.h" | ||
#include "k2/csrc/util.h" | ||
#include "k2/csrc/util/logging.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you were going to get rid of the directory util/? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mm.. there is more code than I realized, now wondering whether we should keep util/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expect more subdirs would be here, as k2 surely needs more function ((IDK) IO, bin ..) that should distribute more organized. And I think the dlpack related codes under Anyway, I hold no opinion about the right project structure🤗. Changes happen fast. So, |
||
|
||
namespace k2 { | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
|
||
#include <stdlib.h> | ||
|
||
#include "glog/logging.h" | ||
#include "k2/csrc/util/logging.h" | ||
|
||
namespace k2 { | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
#--------------------------------- non-glog (loguru) ------------------------------- | ||
if(NOT K2_USE_GLOG) | ||
add_library(non-glog | ||
./logging.cc | ||
./loguru/loguru.cc) | ||
|
||
target_include_directories(non-glog PUBLIC ${CMAKE_SOURCE_DIR}) | ||
|
||
find_package(Threads) | ||
target_link_libraries(non-glog ${CMAKE_THREAD_LIBS_INIT}) # For pthreads | ||
if(NOT WIN32) | ||
target_link_libraries(non-glog dl) # For ldl | ||
endif() | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// k2/csrc/util/logging.h | ||
|
||
// Copyright (c) 2020 Xiaomi Corporation ( authors: Meixu Song ) | ||
|
||
// See ../../LICENSE for clarification regarding multiple authors | ||
|
||
#include "k2/csrc/util/logging.h" | ||
|
||
#ifdef K2_USE_GLOG | ||
// Google glog's api does not have an external function that allows one to check | ||
// if glog is initialized or not. It does have an internal function - so we are | ||
// declaring it here. This is a hack but has been used by a bunch of others too. | ||
// (e.g. Torch). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's better if you add the corresponding code link of |
||
namespace google { | ||
namespace glog_internal_namespace_ { | ||
bool IsGoogleLoggingInitialized(); | ||
} // namespace glog_internal_namespace_ | ||
} // namespace google | ||
|
||
namespace k2 { | ||
bool InitK2Logging(int* argc, char** argv) { | ||
if (*argc == 0) | ||
return true; | ||
#if !defined(_MSC_VER) | ||
// This trick can only be used on UNIX platforms | ||
if (!::google::glog_internal_namespace_::IsGoogleLoggingInitialized()) | ||
#endif | ||
{ | ||
::google::InitGoogleLogging(argv[0]); | ||
#if !defined(_MSC_VER) | ||
// This is never defined on Windows | ||
::google::InstallFailureSignalHandler(); | ||
#endif | ||
} | ||
UpdateLoggingLevelsFromFlags(); | ||
return true; | ||
} | ||
|
||
void UpdateLoggingLevelsFromFlags() { | ||
// TODO(meixu): set some default FLAGS/gflags here | ||
} | ||
} // namespace k2 | ||
#else // !K2_USE_GLOG | ||
namespace k2 { | ||
bool InitK2Logging(int *argc, char **argv) { | ||
if (*argc == 0) | ||
return true; | ||
|
||
loguru::init(*argc, argv); | ||
return true; | ||
} | ||
|
||
void UpdateLoggingLevelsFromFlags() {} | ||
} // namespace k2 | ||
#endif // !K2_USE_GLOG |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// k2/csrc/util/logging_is_google_glog.h | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. filename wrong? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This filename is referred from pytorch, and I think it's clear (TF use |
||
|
||
// Copyright (c) 2020 Xiaomi Corporation (authors: Meixu Song) | ||
|
||
// See ../../LICENSE for clarification regarding multiple authors | ||
|
||
#ifndef K2_CSRC_UTIL_LOGGING_H_ | ||
#define K2_CSRC_UTIL_LOGGING_H_ | ||
|
||
// Choose one logging implementation. | ||
// All have the same common API of google/glog | ||
#ifdef K2_USE_GLOG | ||
#include "k2/csrc/util/logging_is_google_glog.h" | ||
#else | ||
#include "k2/csrc/util/logging_is_not_google_glog.h" | ||
#endif | ||
|
||
namespace k2 { | ||
|
||
// Functions that we use for initialization. | ||
bool InitK2Logging(int* argc, char** argv); | ||
void UpdateLoggingLevelsFromFlags(); | ||
|
||
constexpr bool IsUsingGoogleLogging() { | ||
#ifdef K2_USE_GLOG | ||
return true; | ||
#else | ||
return false; | ||
#endif | ||
} | ||
|
||
} // namespace k2 | ||
#endif // K2_CSRC_UTIL_LOGGING_H_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this off by default? Since we install gtest by default, doesn't that already require glog?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@songmeixu
when to enable this option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OFF is set for test, and help figure out what we need for logging that is outside a minimal one.
The gtest doesn't depend on glog, the former is for unit test, while the latter is for logging. Though the implementations are similar and very simple for some cases (*_EQ), I think there is a design principle to isolate these two.