Skip to content

Commit

Permalink
refactor(logs): use glog instead of prints
Browse files Browse the repository at this point in the history
Ref: HICN-788
Signed-off-by: Enrico Loparco (eloparco) <[email protected]>
Change-Id: Iedf75e1658a335985cc2dfd7b82ae61124f2371e
  • Loading branch information
Enrico Loparco (eloparco) committed Sep 21, 2022
1 parent 10e0758 commit 582e9a1
Show file tree
Hide file tree
Showing 27 changed files with 555 additions and 592 deletions.
14 changes: 13 additions & 1 deletion apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ set(LIBHTTP_PROXY_STATIC ${LIBHTTP_PROXY}.static)
find_package(Threads REQUIRED)
find_package(Libconfig++ ${LIBCONFIG_DEFAULT_VERSION} REQUIRED)


##############################################################
# Check if building as subproject or as root project
##############################################################
Expand Down Expand Up @@ -85,6 +84,19 @@ else()
list(APPEND DEPENDENCIES
${LIBTRANSPORT_LIBRARIES}
)

# glog
list(APPEND THIRD_PARTY_INCLUDE_DIRS
${glog_BINARY_DIR}
${glog_SOURCE_DIR}/src
)
list(APPEND THIRD_PARTY_DEPENDENCIES
glog
)

set(COMMON_INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/common-includes
)
endif()


Expand Down
46 changes: 46 additions & 0 deletions apps/common-includes/hicn/apps/utils/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2022 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <glog/logging.h>

#include <iostream>

#define LoggerInfo() LOG(INFO)
#define LoggerWarn() LOG(WARNING)
#define LoggerErr() LOG(ERROR)
#define LoggerFatal() LOG(FATAL)
#define LoggerVerbose(level) VLOG((level))
#define LoggerIsOn(level) VLOG_IS_ON((level))

struct HicnLogger {
HicnLogger() {
// Set log level
const char *log_level = std::getenv("LOG_LEVEL");
if (log_level != nullptr) FLAGS_v = std::stol(std::string(log_level));

// Enable/disable prefix
const char *enable_log_prefix = std::getenv("ENABLE_LOG_PREFIX");
if (enable_log_prefix != nullptr &&
std::string(enable_log_prefix) == "OFF") {
FLAGS_log_prefix = false;
}

FLAGS_colorlogtostderr = true;
}
};

static HicnLogger logger;
6 changes: 4 additions & 2 deletions apps/higet/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021 Cisco and/or its affiliates.
# Copyright (c) 2021-2022 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
Expand Down Expand Up @@ -49,7 +49,9 @@ if (NOT DISABLE_EXECUTABLES)
${CMAKE_THREAD_LIBS_INIT}
${WSOCK32_LIBRARY}
${WS2_32_LIBRARY}
DEPENDS ${LIBTRANSPORT_LIBRARIES}
INCLUDE_DIRS
PRIVATE ${THIRD_PARTY_INCLUDE_DIRS} ${COMMON_INCLUDE_DIRS}
DEPENDS ${LIBTRANSPORT_LIBRARIES} ${THIRD_PARTY_DEPENDENCIES}
COMPONENT ${HICN_APPS}
DEFINITIONS ${COMPILER_DEFINITIONS}
LINK_FLAGS ${LINK_FLAGS}
Expand Down
37 changes: 17 additions & 20 deletions apps/higet/higet.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Cisco and/or its affiliates.
* Copyright (c) 2021-2022 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
Expand All @@ -13,6 +13,7 @@
* limitations under the License.
*/

#include <hicn/apps/utils/logger.h>
#include <hicn/transport/http/client_connection.h>
#include <hicn/transport/utils/chrono_typedefs.h>

Expand Down Expand Up @@ -172,7 +173,7 @@ class ReadBytesCallbackImplementation
}

print_bar(100, 100, true);
std::cout << "\nDownloaded " << bytes << " bytes" << std::endl;
LoggerInfo() << "\nDownloaded " << bytes << " bytes";
}
work_.reset();
});
Expand Down Expand Up @@ -216,8 +217,7 @@ class ReadBytesCallbackImplementation
}
}
if (last) {
std::cout << "] " << int(progress * 100.0) << " %" << std::endl
<< std::endl;
std::cout << "] " << int(progress * 100.0) << " %";
} else {
std::cout << "] " << int(progress * 100.0) << " %\r";
std::cout.flush();
Expand Down Expand Up @@ -250,20 +250,17 @@ long checkFileStatus(std::string file_name) {
}

void usage(char *program_name) {
std::cerr << "usage:" << std::endl;
std::cerr << program_name << " [option]... [url]..." << std::endl;
std::cerr << program_name << " options:" << std::endl;
std::cerr
<< "-O <out_put_path> = write documents to <out_put_file>"
<< std::endl;
std::cerr << "-S = print server response"
<< std::endl;
std::cerr << "-P = first word of the ipv6 name of "
"the response"
<< std::endl;
std::cerr << "example:" << std::endl;
std::cerr << "\t" << program_name << " -O - http://origin/index.html"
<< std::endl;
LoggerInfo() << "usage:";
LoggerInfo() << program_name << " [option]... [url]...";
LoggerInfo() << program_name << " options:";
LoggerInfo()
<< "-O <out_put_path> = write documents to <out_put_file>";
LoggerInfo() << "-S = print server response";
LoggerInfo()
<< "-P = first word of the ipv6 name of "
"the response";
LoggerInfo() << "example:";
LoggerInfo() << "\t" << program_name << " -O - http://origin/index.html";
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -308,8 +305,8 @@ int main(int argc, char **argv) {
}

name = argv[optind];
std::cerr << "Using name " << name << " and name first word "
<< conf.ipv6_first_word << std::endl;
LoggerInfo() << "Using name " << name << " and name first word "
<< conf.ipv6_first_word;

if (conf.file_name.empty()) {
conf.file_name = name.substr(1 + name.find_last_of("/"));
Expand Down
3 changes: 2 additions & 1 deletion apps/hiperf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ if (NOT DISABLE_EXECUTABLES)
INCLUDE_DIRS
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src
PRIVATE ${LIBCONFIG_CPP_INCLUDE_DIRS}
DEPENDS ${DEPENDENCIES}
PRIVATE ${THIRD_PARTY_INCLUDE_DIRS} ${COMMON_INCLUDE_DIRS}
DEPENDS ${DEPENDENCIES} ${THIRD_PARTY_DEPENDENCIES}
COMPONENT ${HICN_APPS}
LINK_FLAGS ${LINK_FLAGS}
COMPILE_OPTIONS ${COMPILER_OPTIONS}
Expand Down
14 changes: 7 additions & 7 deletions apps/hiperf/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <hicn/transport/utils/daemonizator.h>
#endif

#include <hicn/apps/utils/logger.h>

#include <asio.hpp>
#include <cmath>
#include <fstream>
Expand Down Expand Up @@ -57,8 +59,6 @@ namespace hiperf {
/**
* Logger
*/
static std::ostream &Logger() { return std::cout; }

template <typename D, typename ConfType, typename ParentType>
class Base : protected std::stringbuf, protected std::ostream {
protected:
Expand Down Expand Up @@ -103,7 +103,7 @@ class Base : protected std::stringbuf, protected std::ostream {
int sync() override {
auto string = str();
asio::post(io_service_,
[this, string]() { Logger() << begin_ << string << end_; });
[this, string]() { LoggerInfo() << begin_ << string << end_; });
str("");

return 0;
Expand Down Expand Up @@ -140,7 +140,7 @@ static inline int ensureFlows(const Prefix &prefix, std::size_t flows) {
} else if (prefix.getAddressFamily() == AF_INET6) {
max_ip_addr_len_bits = IPV6_ADDR_LEN_BITS;
} else {
Logger() << "Error: unknown address family." << std::endl;
LoggerErr() << "Error: unknown address family.";
ret = ERROR_SETUP;
goto end;
}
Expand All @@ -149,9 +149,9 @@ static inline int ensureFlows(const Prefix &prefix, std::size_t flows) {
max_n_flow = log2_n_flow < 64 ? (1 << log2_n_flow) : ~0ULL;

if (flows > max_n_flow) {
Logger() << "Error: the provided prefix length does not allow to "
"accomodate the provided number of flows ("
<< flows << " > " << max_n_flow << ")." << std::endl;
LoggerErr() << "Error: the provided prefix length does not allow to "
"accomodate the provided number of flows ("
<< flows << " > " << max_n_flow << ").";
ret = ERROR_SETUP;
}

Expand Down
Loading

0 comments on commit 582e9a1

Please sign in to comment.