Skip to content

Commit

Permalink
NC | Noobaa syslogs are sent to two different files
Browse files Browse the repository at this point in the history
Signed-off-by: naveenpaul1 <[email protected]>
(cherry picked from commit 8b310d6)
  • Loading branch information
naveenpaul1 authored and romayalon committed Jul 23, 2024
1 parent 75de456 commit 06839aa
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 16 deletions.
6 changes: 5 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const _ = require('lodash');
const util = require('util');
const nsfs_schema_utils = require('./src/manage_nsfs/nsfs_schema_utils');
const range_utils = require('./src/util/range_utils');
const { EventEmitter } = require('events');

config.event_emitter = new EventEmitter();

/////////////////////////
// CONTAINER RESOURCES //
Expand Down Expand Up @@ -464,8 +467,8 @@ config.EVENT_FACILITY = 'LOG_LOCAL2';
config.EVENT_LOGGING_ENABLED = true;
config.EVENT_LEVEL = 5;

config.LOG_TO_SYSLOG_ENABLED = true;
config.LOG_TO_STDERR_ENABLED = true;
config.LOG_TO_SYSLOG_ENABLED = false;

// TEST Mode
config.test_mode = false;
Expand Down Expand Up @@ -1078,6 +1081,7 @@ function load_nsfs_nc_config() {
});
console.warn(`nsfs: config_dir_path=${config.NSFS_NC_CONF_DIR} config.json= ${util.inspect(merged_config)}`);
validate_nc_master_keys_config(config);
config.event_emitter.emit("config_updated");
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND' && err.code !== 'ENOENT') throw err;
console.warn('config.load_nsfs_nc_config could not find config.json... skipping');
Expand Down
14 changes: 13 additions & 1 deletion src/native/fs/fs_napi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2123,7 +2123,18 @@ set_debug_level(const Napi::CallbackInfo& info)
{
int level = info[0].As<Napi::Number>();
DBG_SET_LEVEL(level);
DBG1("FS::set_debug_level " << level);
LOG("FS::set_debug_level " << level);
return info.Env().Undefined();
}

static Napi::Value
set_log_config(const Napi::CallbackInfo& info)
{
bool stderr_enabled = info[0].As<Napi::Boolean>();
bool syslog_enabled = info[1].As<Napi::Boolean>();
LOG_TO_STDERR_ENABLED = stderr_enabled;
LOG_TO_SYSLOG_ENABLED = syslog_enabled;
LOG("FS::set_log_config: " << DVAL(LOG_TO_STDERR_ENABLED) << DVAL(LOG_TO_SYSLOG_ENABLED));
return info.Env().Undefined();
}

Expand Down Expand Up @@ -2260,6 +2271,7 @@ fs_napi(Napi::Env env, Napi::Object exports)

exports_fs["dio_buffer_alloc"] = Napi::Function::New(env, dio_buffer_alloc);
exports_fs["set_debug_level"] = Napi::Function::New(env, set_debug_level);
exports_fs["set_log_config"] = Napi::Function::New(env, set_log_config);

exports["fs"] = exports_fs;
}
Expand Down
2 changes: 2 additions & 0 deletions src/native/nb_native.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
'util/struct_buf.h',
'util/struct_buf.cpp',
'util/common.h',
'util/common.cpp',
'util/napi.h',
'util/napi.cpp',
'util/os.h',
Expand Down Expand Up @@ -104,6 +105,7 @@
'util/buf.cpp',
'util/buf.h',
'util/common.h',
'util/common.cpp',
'util/compression.cpp',
'util/compression.h',
'util/gf2.h',
Expand Down
7 changes: 7 additions & 0 deletions src/native/util/common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "common.h"

namespace noobaa
{
bool LOG_TO_STDERR_ENABLED = true;
bool LOG_TO_SYSLOG_ENABLED = false;
}
40 changes: 34 additions & 6 deletions src/native/util/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "backtrace.h"
#include "os.h"
#include "syslog.h"

namespace noobaa
{
Expand All @@ -28,19 +29,46 @@ namespace noobaa

#define DVAL(x) #x "=" << x << " "

#define LOG(x) std::cerr << LOG_PREFIX() << x << std::endl
extern bool LOG_TO_STDERR_ENABLED;
extern bool LOG_TO_SYSLOG_ENABLED;

#define STD_LOG(x) \
do { \
std::cerr << x << std::endl; \
} while (0)

#define SYS_LOG(x) \
do { \
const char* log_msg = x.c_str(); \
int facility = LOG_LOCAL0; \
int priority = 0; \
::syslog(priority | facility, "%s", log_msg); \
} while (0)

#define LOG(x) \
do { \
std::ostringstream oss; \
oss << "" << LOG_PREFIX() << x; \
std::string message = oss.str(); \
if (LOG_TO_STDERR_ENABLED) { \
STD_LOG(message); \
} \
if (LOG_TO_SYSLOG_ENABLED) { \
SYS_LOG(message); \
} \
} while (0)

// to use DBG the module/file should use either DBG_INIT or DBG_INIT_VAR.
#define DBG_INIT(level) static int __module_debug_var__ = level
#define DBG_INIT_VAR(debug_var) static int& __module_debug_var__ = debug_var
#define DBG_SET_LEVEL(level) __module_debug_var__ = level
#define DBG_GET_LEVEL() (__module_debug_var__)
#define DBG_VISIBLE(level) (level <= __module_debug_var__)
#define DBG(level, x) \
do { \
if (DBG_VISIBLE(level)) { \
LOG("[L" << level << "] " << x); \
} \
#define DBG(level, x) \
do { \
if (DBG_VISIBLE(level)) { \
LOG("[L" << level << "] " << x); \
} \
} while (0)
#define DBG0(x) DBG(0, x)
#define DBG1(x) DBG(1, x)
Expand Down
1 change: 1 addition & 0 deletions src/sdk/nb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ interface NativeFS {

dio_buffer_alloc(size: number): Buffer;
set_debug_level(level: number);
set_log_config(stderr_enabled: boolean, syslog_enabled: boolean,);

S_IFMT: number;
S_IFDIR: number;
Expand Down
1 change: 0 additions & 1 deletion src/server/bg_services/semaphore_monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class SemaphoreMonitor {
}

async run_batch() {
dbg.log1("semaphore_monitor: START");
if (!this._can_run()) return;
try {
this.run_semaphore_monitor();
Expand Down
15 changes: 8 additions & 7 deletions src/util/debug_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ util.inspect.defaultOptions.depth = 10;
util.inspect.defaultOptions.colors = true;
util.inspect.defaultOptions.breakLength = Infinity;


//Detect our context, node/atom/browser
//Different context requires different handling, for example rotating file steam usage or console wrapping
let syslog;
Expand Down Expand Up @@ -586,10 +585,12 @@ if (console_wrapper) {
console_wrapper.register_logger(conlogger);
}

let native_log_level = LOG_LEVEL;
if (process.env.NOOBAA_LOG_LEVEL) {
native_log_level = dbg_conf.level;
}
if (Number(native_log_level)) {
nb_native().fs.set_debug_level(Number(native_log_level));

function set_log_config() {
const dbg_native_conf = debug_config.get_debug_config(process.env.NOOBAA_LOG_LEVEL);
nb_native().fs.set_debug_level(dbg_native_conf.level);
nb_native().fs.set_log_config(config.LOG_TO_STDERR_ENABLED, config.LOG_TO_SYSLOG_ENABLED);
}

config.event_emitter.on("config_updated", set_log_config);
set_log_config();

0 comments on commit 06839aa

Please sign in to comment.