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

add a logging alternative #80

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,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)
Copy link
Collaborator

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?

Copy link
Collaborator

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?

Copy link
Collaborator Author

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.


enable_testing()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
include(cpplint)
if(K2_USE_GLOG)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(K2_USE_GLOG)
  include(glog)
endif()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed.

include(glog)
endif()
include(googletest)
include(pybind11)

Expand Down
1 change: 1 addition & 0 deletions k2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(util)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please put it into csrc. No need to create a new directory.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed RE no need for new directory.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap.

add_subdirectory(csrc)
add_subdirectory(python)
44 changes: 22 additions & 22 deletions k2/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# please sort the source files alphabetically
add_library(fsa
arcsort.cc
aux_labels.cc
connect.cc
determinize.cc
fsa.cc
fsa_equivalent.cc
fsa_renderer.cc
fsa_util.cc
intersect.cc
properties.cc
rmepsilon.cc
topsort.cc
util.cc
weights.cc
)
set(FSA_SOURCES
arcsort.cc
aux_labels.cc
connect.cc
determinize.cc
fsa.cc
fsa_equivalent.cc
fsa_renderer.cc
fsa_util.cc
intersect.cc
properties.cc
rmepsilon.cc
topsort.cc
util.cc
weights.cc)

add_library(fsa ${FSA_SOURCES})

target_include_directories(fsa PUBLIC ${CMAKE_SOURCE_DIR})
if(CMAKE_VERSION VERSION_LESS 3.8)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if(K2_USE_GLOG)
target_compile_definitions(fsa PRIVATE K2_USE_GLOG)
target_link_libraries(fsa PUBLIC glog)
else()
target_compile_features(fsa PUBLIC cxx_std_11)
target_link_libraries(fsa PUBLIC non-glog)
endif()
target_link_libraries(fsa PUBLIC glog)

function(k2_add_fsa_test name)
add_executable(${name} "${name}.cc")
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/arcsort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <utility>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please follow the code style and sort the headers
in alphabetic order.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All filenames should be in lowercase.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's what may differ with logging.* in glog. But on case-insensitive platforms (windows), it's useless. Thus I changed to lowercase.

Changed to the alphabetic order.

#include "k2/csrc/fsa.h"

namespace k2 {
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/arcsort.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"

namespace k2 {
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <utility>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"

namespace k2 {

Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/aux_labels.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <utility>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"
#include "k2/csrc/fsa_util.h"
#include "k2/csrc/properties.h"
Expand Down
5 changes: 2 additions & 3 deletions k2/csrc/connect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

// See ../../LICENSE for clarification regarding multiple authors

#include "k2/csrc/connect.h"

#include <algorithm>
#include <limits>
#include <stack>
#include <unordered_map>
#include <vector>

#include "glog/logging.h"
#include "k2/csrc/connect.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The corresponding header file (i.e. connect.h here) should be put at the top of .cc file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"
#include "k2/csrc/fsa_util.h"
#include "k2/csrc/properties.h"
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/dense_fsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <utility>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"
#include "k2/csrc/util.h"

Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/determinize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <utility>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"
#include "k2/csrc/fsa_util.h"
#include "k2/csrc/properties.h"
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/determinize.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <utility>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/determinize_impl.h"
#include "k2/csrc/fsa.h"
#include "k2/csrc/util.h"
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/fsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <utility>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/array.h"
#include "k2/csrc/util.h"

Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/fsa_equivalent.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <cstdint>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"
#include "k2/csrc/weights.h"

Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/fsa_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <utility>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/connect.h"
#include "k2/csrc/properties.h"
#include "k2/csrc/util.h"
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/intersect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <utility>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"
#include "k2/csrc/properties.h"
#include "k2/csrc/util.h"
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/intersect.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"

namespace k2 {
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/rmepsilon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <unordered_map>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"
#include "k2/csrc/properties.h"
#include "k2/csrc/util.h"
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/rmepsilon.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <utility>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/determinize_impl.h"
#include "k2/csrc/fsa.h"
#include "k2/csrc/weights.h"
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/topsort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <unordered_map>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"
#include "k2/csrc/properties.h"
#include "k2/csrc/util.h"
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/topsort.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <utility>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"

namespace k2 {
Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <stdlib.h>

#include "glog/logging.h"
#include "k2/util/Logging.h"

namespace k2 {

Expand Down
2 changes: 1 addition & 1 deletion k2/csrc/weights.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <queue>
#include <vector>

#include "glog/logging.h"
#include "k2/util/Logging.h"
#include "k2/csrc/fsa.h"
#include "k2/csrc/properties.h"
#include "k2/csrc/util.h"
Expand Down
2 changes: 1 addition & 1 deletion k2/python/csrc/tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "k2/python/csrc/tensor.h"

#include "glog/logging.h"
#include "k2/util/Logging.h"

namespace k2 {

Expand Down
15 changes: 15 additions & 0 deletions k2/util/CMakeLists.txt
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()
57 changes: 57 additions & 0 deletions k2/util/Logging.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// $FILE_PATHNAME $HEADER_FILENAME $HEADER_PATHNAME
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please replace this line with the one used by other k2 files.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// Copyright (c) 2020 Xiaomi Corporation ( authors: Meixu Song )
//

// See ../../LICENSE for clarification regarding multiple authors

#include "k2/util/Logging.h"

#ifdef K2_USE_GLOG
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to use glog directly? K2 is a training framework and I don't think
mobile devices have the power for training. As far as I know, glog
is supported on Linux, macOS and Windows.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, k2 would be used in inference as well.
Of course that will require some things to be worked on!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For mobile is one thing, the other more important reason is about glog itself actually. After investigations, I found glog is not beautiful as I thought. Even the logging system itself is a trade-off/confusing concept, that would easily be noisy, expose too much global flags/macro, takes too much responsibility (exception/error handling). So in conclusion, I found there isn't the best practice. However, glog with one minimal glog-like logging is preferred by many great projects. A minimal glog could be very simple to just support minimal functions of glog. And loguru turn out to be the expected one. So, with loguru, the two-logger thought is implemented as this PR.

// 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RE has been used by a bunch of others, it's better if we could list some famous project here with the corresponding code links.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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) {
// When doing InitCaffeLogging, we will assume that caffe's flag parser has
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the comment contain caffe?
Is the code copied from caffe? Please add some comment
in the code to indicate where it is from.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the argc,argv stuff may not be necessary as I don't anticipate using k2 much in command line programs.

Copy link
Collaborator Author

@megazone87 megazone87 Aug 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emm, let me explain this. Before coding, I investigated pytorch, tf, bazel, and other projects that make use of glog, and many alternative logging methods. To find one best practice for logging, some common thought is used here. I try to keep minimal and non-specific to some project codes here. As here, it declares an internal function of glog, which is a common hack as it said, so I leave it as it is. However, this comment shouldn't be here. As the loguru here does parse the arg. The comment is removed now.


To @danpovey :
This function is necessary to init both loggers. At least, to configure the log to stdout or logfile. So I prefer to keep it right now and make the changes we need later.

// already finished.
if (*argc == 0)
return true;

loguru::init(*argc, argv);
Copy link
Collaborator

@csukuangfj csukuangfj Aug 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name of functions should be in Uppercase. init->Init.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the name init is from loguru

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is from loguru.

return true;
}

void UpdateLoggingLevelsFromFlags() {}
} // namespace k2
#endif // !K2_USE_GLOG
32 changes: 32 additions & 0 deletions k2/util/Logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the first line of every file should be the path to the file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// Created by songmeixu (songmeixu@outlook.com) on 2020/8/3.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the header style of other files.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@songmeixu And I think it's better if you declare your copyright under Xiaomi Corporation, as Dan and I did.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it's from my IDE old setting, and I forget it in some files. Changes have been made referring to the existing style.

// Copyright (c) 2020 Xiaomi Inc. All rights reserved.
//

#ifndef K2_UTIL_LOGGING_H_
#define K2_UTIL_LOGGING_H_

// Choose one logging implementation.
// All have the same common API of google/glog
#ifdef K2_USE_GLOG
#include "k2/util/logging_is_google_glog.h"
#else
#include "k2/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_UTIL_LOGGING_H_
Loading