Skip to content

Commit

Permalink
feat: Distinguish log file names
Browse files Browse the repository at this point in the history
  • Loading branch information
acelyc111 committed May 27, 2024
1 parent 7edbb2d commit 67b5b12
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 47 deletions.
6 changes: 3 additions & 3 deletions src/failure_detector/test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ while read -r -a line; do
echo "run dsn.failure_detector.tests $test_case failed"
echo "---- ls ----"
ls -l
if find . -name log.1.txt; then
echo "---- tail -n 100 log.1.txt ----"
tail -n 100 `find . -name log.1.txt`
if [ `find . -name pegasus.log.* | wc -l` -ne 0 ]; then
echo "---- tail -n 100 pegasus.log.* ----"
tail -n 100 `find . -name pegasus.log.*`
fi
if [ -f core ]; then
echo "---- gdb ./dsn.failure_detector.tests core ----"
Expand Down
6 changes: 3 additions & 3 deletions src/meta/test/meta_state/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ while read -r -a line; do
echo "run dsn_meta_state_tests $test_case failed"
echo "---- ls ----"
ls -l
if find . -name log.1.txt; then
echo "---- tail -n 100 log.1.txt ----"
tail -n 100 `find . -name log.1.txt`
if [ `find . -name pegasus.log.* | wc -l` -ne 0 ]; then
echo "---- tail -n 100 pegasus.log.* ----"
tail -n 100 `find . -name pegasus.log.*`
fi
if [ -f core ]; then
echo "---- gdb ./dsn_meta_state_tests core ----"
Expand Down
6 changes: 3 additions & 3 deletions src/meta/test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ if [ $? -ne 0 ]; then
echo "run dsn.meta.test failed"
echo "---- ls ----"
ls -l
if find . -name log.1.txt; then
echo "---- tail -n 100 log.1.txt ----"
tail -n 100 `find . -name log.1.txt`
if [ `find . -name pegasus.log.* | wc -l` -ne 0 ]; then
echo "---- tail -n 100 pegasus.log.* ----"
tail -n 100 `find . -name pegasus.log.*`
fi
if [ -f core ]; then
echo "---- gdb ./dsn.meta.test core ----"
Expand Down
2 changes: 1 addition & 1 deletion src/replica/backup/test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fi
./dsn_replica_backup_test

if [ $? -ne 0 ]; then
tail -n 100 data/log/log.1.txt
tail -n 100 `find . -name pegasus.log.*`
if [ -f core ]; then
gdb ./dsn_replica_backup_test core -ex "bt"
fi
Expand Down
2 changes: 1 addition & 1 deletion src/replica/bulk_load/test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fi
./dsn_replica_bulk_load_test

if [ $? -ne 0 ]; then
tail -n 100 data/log/log.1.txt
tail -n 100 `find . -name pegasus.log.*`
if [ -f core ]; then
gdb ./dsn_replica_bulk_load_test core -ex "bt"
fi
Expand Down
2 changes: 1 addition & 1 deletion src/replica/duplication/test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fi
./dsn_replica_dup_test

if [ $? -ne 0 ]; then
tail -n 100 data/log/log.1.txt
tail -n 100 `find . -name pegasus.log.*`
if [ -f core ]; then
gdb ./dsn_replica_dup_test core -ex "bt"
fi
Expand Down
2 changes: 1 addition & 1 deletion src/replica/split/test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fi
./dsn_replica_split_test

if [ $? -ne 0 ]; then
tail -n 100 data/log/log.1.txt
tail -n 100 `find . -name pegasus.log.*`
if [ -f core ]; then
gdb ./dsn_replica_split_test core -ex "bt"
fi
Expand Down
6 changes: 3 additions & 3 deletions src/replica/storage/simple_kv/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ if [ -f core ] || ! grep ERR_OK out > /dev/null ; then
ls -l
echo "---- head -n 100 out ----"
head -n 100 out
if [ -f data/logs/log.1.txt ]; then
echo "---- tail -n 100 log.1.txt ----"
tail -n 100 data/logs/log.1.txt
if [ `find data/logs -name pegasus.log.* | wc -l` -ne 0 ]; then
echo "---- tail -n 100 pegasus.log.* ----"
tail -n 100 `find data/logs -name pegasus.log.*`
fi
if [ -f core ]; then
echo "---- gdb ./dsn.replication.simple_kv core ----"
Expand Down
4 changes: 2 additions & 2 deletions src/replica/storage/simple_kv/test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ function run_single()
echo "${bin} ${prefix}.ini ${prefix}.act"
${bin} ${prefix}.ini ${prefix}.act
ret=$?
if find . -name log.1.txt &>/dev/null; then
log=`find . -name log.1.txt`
if [ `find . -name pegasus.log.* | wc -l` -ne 0 ]; then
log=`find . -name pegasus.log.*`
cat ${log} | grep -v FAILURE_DETECT | grep -v BEACON | grep -v beacon | grep -v THREAD_POOL_FD >${prefix}.log
rm ${log}
fi
Expand Down
25 changes: 20 additions & 5 deletions src/runtime/service_api_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#endif

#include "fmt/core.h"
#include "fmt/format.h"
#include "perf_counter/perf_counters.h"
#include "runtime/api_layer1.h"
#include "runtime/api_task.h"
Expand Down Expand Up @@ -457,8 +458,25 @@ bool run(const char *config_file,
::MallocExtension::instance()->SetMemoryReleaseRate(FLAGS_tcmalloc_release_rate);
#endif

// init logging
dsn_log_init(spec.logging_factory_name, spec.log_dir, dsn_log_prefixed_message_func);
// Extract app_names.
std::list<std::string> app_names_and_indexes;
::dsn::utils::split_args(app_list.c_str(), app_names_and_indexes, ';');
std::vector<std::string> app_names;
for (const auto &app_name_and_index : app_names_and_indexes) {
std::vector<std::string> name_and_index;
::dsn::utils::split_args(app_name_and_index.c_str(), name_and_index, '@');
if (name_and_index.empty()) {
fmt::print(stderr, "app_name should be specified in '{}'", app_name_and_index);
return false;
}
app_names.push_back(name_and_index[0]);
}
// Initialize logging.
dsn_log_init(spec.logging_factory_name,
spec.log_dir,
fmt::format("{}", fmt::join(app_names, ".")),
dsn_log_prefixed_message_func);
// Prepare the minimum necessary.
::dsn::service_engine::instance().init_before_toollets(spec);
Expand Down Expand Up @@ -513,9 +531,6 @@ bool run(const char *config_file,
if (app_list.empty()) {
create_it = true;
} else {
// Extract app_names.
std::list<std::string> app_names_and_indexes;
::dsn::utils::split_args(app_list.c_str(), app_names_and_indexes, ';');
for (const auto &app_name_and_index : app_names_and_indexes) {
std::vector<std::string> name_and_index;
::dsn::utils::split_args(app_name_and_index.c_str(), name_and_index, '@');
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ while read -r -a line; do
echo "run dsn_runtime_tests $test_case failed"
echo "---- ls ----"
ls -l
if find . -name log.1.txt; then
echo "---- tail -n 100 log.1.txt ----"
tail -n 100 `find . -name log.1.txt`
if [ `find . -name pegasus.log.* | wc -l` -ne 0 ]; then
echo "---- tail -n 100 pegasus.log.* ----"
tail -n 100 `find . -name pegasus.log.*`
fi
if [ -f core ]; then
echo "---- gdb ./dsn_runtime_tests core ----"
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ void tracer::install(service_spec &spec)
"tracer.find",
"Find related logs",
"[forward|f|backward|b] [rpc|r|task|t] [trace_id|task_id(e.g., a023003920302390)] "
"<log_file_name(e.g., log.yyyyMMdd_hhmmss_SSS)>",
"<log_file_name(e.g., replica.log.yyyyMMdd_hhmmss_SSS)>",
tracer_log_flow);
});
}
Expand Down
8 changes: 5 additions & 3 deletions src/utils/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <functional>
#include <memory>
#include <string>
#include <utility>

#include "runtime/tool_api.h"
#include "simple_logger.h"
Expand Down Expand Up @@ -61,7 +62,7 @@ std::function<std::string()> log_prefixed_message_func = []() -> std::string { r

void set_log_prefixed_message_func(std::function<std::string()> func)
{
log_prefixed_message_func = func;
log_prefixed_message_func = std::move(func);
}
} // namespace dsn

Expand All @@ -73,7 +74,8 @@ static void log_on_sys_exit(::dsn::sys_exit_type)

void dsn_log_init(const std::string &logging_factory_name,
const std::string &log_dir,
std::function<std::string()> dsn_log_prefixed_message_func)
const std::string &role_name,
const std::function<std::string()> &dsn_log_prefixed_message_func)
{
log_start_level = enum_from_string(FLAGS_logging_start_level, LOG_LEVEL_INVALID);

Expand All @@ -86,7 +88,7 @@ void dsn_log_init(const std::string &logging_factory_name,
}

dsn::logging_provider *logger = dsn::utils::factory_store<dsn::logging_provider>::create(
logging_factory_name.c_str(), dsn::PROVIDER_TYPE_MAIN, log_dir.c_str());
logging_factory_name.c_str(), dsn::PROVIDER_TYPE_MAIN, log_dir.c_str(), role_name.c_str());
dsn::logging_provider::set_logger(logger);

if (dsn_log_prefixed_message_func != nullptr) {
Expand Down
9 changes: 5 additions & 4 deletions src/utils/logging_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class logging_provider
{
public:
template <typename T>
static logging_provider *create(const char *log_dir)
static logging_provider *create(const char *log_dir, const char *role_name)
{
return new T(log_dir);
return new T(log_dir, role_name);
}

typedef logging_provider *(*factory)(const char *);
typedef logging_provider *(*factory)(const char *, const char *);

public:
virtual ~logging_provider() = default;
Expand Down Expand Up @@ -88,4 +88,5 @@ bool register_component_provider(const char *name,

extern void dsn_log_init(const std::string &logging_factory_name,
const std::string &log_dir,
std::function<std::string()> dsn_log_prefixed_message_func);
const std::string &role_name,
const std::function<std::string()> &dsn_log_prefixed_message_func);
7 changes: 5 additions & 2 deletions src/utils/simple_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ DSN_DEFINE_validator(stderr_start_level, [](const char *value) -> bool {
return LOG_LEVEL_DEBUG <= level && level <= LOG_LEVEL_FATAL;
});

DSN_DEFINE_string(tools.simple_logger, base_name, "pegasus", "The default base name for log file");

DSN_DECLARE_string(logging_start_level);

namespace dsn {
Expand Down Expand Up @@ -204,14 +206,15 @@ void screen_logger::log(

void screen_logger::flush() { ::fflush(stdout); }

simple_logger::simple_logger(const char *log_dir)
simple_logger::simple_logger(const char *log_dir, const char *role_name)
: _log_dir(std::string(log_dir)),
_log(nullptr),
_file_bytes(0),
_stderr_start_level(enum_from_string(FLAGS_stderr_start_level, LOG_LEVEL_INVALID))
{
// Use 'role_name' if it is specified, otherwise use 'base_name'.
const std::string symlink_name("log");
const std::string symlink_name(
fmt::format("{}.log", utils::is_empty(role_name) ? FLAGS_base_name : role_name));
_file_name_prefix = fmt::format("{}.", symlink_name);
_symlink_path = utils::filesystem::path_combine(_log_dir, symlink_name);

Expand Down
5 changes: 4 additions & 1 deletion src/utils/simple_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class screen_logger : public logging_provider
{
public:
explicit screen_logger(bool short_header);
explicit screen_logger(const char *, const char *) : _short_header(true) {}
~screen_logger() override = default;

void log(const char *file,
Expand Down Expand Up @@ -74,7 +75,9 @@ class screen_logger : public logging_provider
class simple_logger : public logging_provider
{
public:
simple_logger(const char *log_dir);
// 'log_dir' is the directory to store log files, 'role_name' is the name of the process,
// such as 'replica_server', 'meta_server' in Pegasus.
simple_logger(const char *log_dir, const char *role_name);
~simple_logger() override;

void log(const char *file,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/test/clear.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
# THE SOFTWARE.


rm -rf dsn.utils.tests.xml log*.txt
rm -rf dsn.utils.tests.xml pegasus*.txt
4 changes: 2 additions & 2 deletions src/utils/test/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class simple_logger_test : public logger_test
ASSERT_TRUE(utils::filesystem::get_subfiles(test_dir, sub_list, false));

file_names.clear();
std::regex pattern(R"(log\.[0-9]{8}_[0-9]{6}_[0-9]{3})");
std::regex pattern(R"(SimpleLogger\.log\.[0-9]{8}_[0-9]{6}_[0-9]{3})");
for (const auto &path : sub_list) {
std::string name(utils::filesystem::get_file_name(path));
if (std::regex_match(name, pattern)) {
Expand Down Expand Up @@ -161,7 +161,7 @@ TEST_F(simple_logger_test, redundant_log_test)
std::set<std::string> before_files;
NO_FATALS(get_log_files(before_files));

auto logger = std::make_unique<simple_logger>(test_dir.c_str());
auto logger = std::make_unique<simple_logger>(test_dir.c_str(), "SimpleLogger");
for (unsigned int i = 0; i != 1000; ++i) {
LOG_PRINT(logger.get(), "{}", "test_print");
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ GTEST_API_ int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);

dsn_log_init("dsn::tools::simple_logger", "./", nullptr);
dsn_log_init("dsn::tools::simple_logger", "./", "test", nullptr);

dsn::flags_initialize();

Expand Down
6 changes: 3 additions & 3 deletions src/utils/test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ if [ $? -ne 0 ]; then
echo "run dsn_utils_tests failed"
echo "---- ls ----"
ls -l
if find . -name log.1.txt; then
echo "---- tail -n 100 log.1.txt ----"
tail -n 100 `find . -name log.1.txt`
if [ `find . -name pegasus.log.* | wc -l` -ne 0 ]; then
echo "---- tail -n 100 pegasus.log.* ----"
tail -n 100 `find . -name pegasus.log.*`
fi
if [ -f core ]; then
echo "---- gdb ./dsn_utils_tests core ----"
Expand Down
6 changes: 3 additions & 3 deletions src/zookeeper/test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ if [ $? -ne 0 ]; then
echo "run dsn.zookeeper.tests failed"
echo "---- ls ----"
ls -l
if find . -name log.1.txt; then
echo "---- tail -n 100 log.1.txt ----"
tail -n 100 `find . -name log.1.txt`
if [ `find . -name pegasus.log.* | wc -l` -ne 0 ]; then
echo "---- tail -n 100 pegasus.log.* ----"
tail -n 100 `find . -name pegasus.log.*`
fi
if [ -f core ]; then
echo "---- gdb ./dsn.zookeeper.tests core ----"
Expand Down

0 comments on commit 67b5b12

Please sign in to comment.