Skip to content

Commit f05e923

Browse files
Merge pull request #8228 from romayalon/romy-5.16.1-backports
5.16.1 Backports
2 parents 1c4ce8b + 06839aa commit f05e923

File tree

11 files changed

+146
-18
lines changed

11 files changed

+146
-18
lines changed

config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const _ = require('lodash');
1414
const util = require('util');
1515
const nsfs_schema_utils = require('./src/manage_nsfs/nsfs_schema_utils');
1616
const range_utils = require('./src/util/range_utils');
17+
const { EventEmitter } = require('events');
18+
19+
config.event_emitter = new EventEmitter();
1720

1821
/////////////////////////
1922
// CONTAINER RESOURCES //
@@ -464,6 +467,9 @@ config.EVENT_FACILITY = 'LOG_LOCAL2';
464467
config.EVENT_LOGGING_ENABLED = true;
465468
config.EVENT_LEVEL = 5;
466469

470+
config.LOG_TO_STDERR_ENABLED = true;
471+
config.LOG_TO_SYSLOG_ENABLED = false;
472+
467473
// TEST Mode
468474
config.test_mode = false;
469475

@@ -1075,6 +1081,7 @@ function load_nsfs_nc_config() {
10751081
});
10761082
console.warn(`nsfs: config_dir_path=${config.NSFS_NC_CONF_DIR} config.json= ${util.inspect(merged_config)}`);
10771083
validate_nc_master_keys_config(config);
1084+
config.event_emitter.emit("config_updated");
10781085
} catch (err) {
10791086
if (err.code !== 'MODULE_NOT_FOUND' && err.code !== 'ENOENT') throw err;
10801087
console.warn('config.load_nsfs_nc_config could not find config.json... skipping');

docs/dev_guide/NonContainerizedDeveloperCustomizations.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,43 @@ Example:
458458
3. systemctl restart noobaa
459459
```
460460

461+
## 25. Syslog enable flag -
462+
**Description -** This flag will enable syslog logging for the application.
463+
464+
**Configuration Key -** LOG_TO_SYSLOG_ENABLED
465+
466+
**Type -** boolean
467+
468+
**Default -** true
469+
470+
**Steps -**
471+
```
472+
1. Open /path/to/config_dir/config.json file.
473+
2. Set the config key -
474+
Example:
475+
"LOG_TO_SYSLOG_ENABLED": true
476+
3. systemctl restart noobaa
477+
```
478+
479+
## 26. Stderr enable flag -
480+
**Description -** This flag will decide whether need to push logs to the stderr or not.
481+
482+
**Configuration Key -** LOG_TO_STDERR_ENABLED
483+
484+
**Type -** boolean
485+
486+
**Default -** false
487+
488+
**Steps -**
489+
```
490+
1. Open /path/to/config_dir/config.json file.
491+
2. Set the config key -
492+
Example:
493+
"LOG_TO_STDERR_ENABLED": false
494+
3. systemctl restart noobaa
495+
```
496+
497+
```
461498
> cat /path/to/config_dir/config.json
462499
{
463500
"ENDPOINT_PORT": 80,

src/native/fs/fs_napi.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,18 @@ set_debug_level(const Napi::CallbackInfo& info)
21232123
{
21242124
int level = info[0].As<Napi::Number>();
21252125
DBG_SET_LEVEL(level);
2126-
DBG1("FS::set_debug_level " << level);
2126+
LOG("FS::set_debug_level " << level);
2127+
return info.Env().Undefined();
2128+
}
2129+
2130+
static Napi::Value
2131+
set_log_config(const Napi::CallbackInfo& info)
2132+
{
2133+
bool stderr_enabled = info[0].As<Napi::Boolean>();
2134+
bool syslog_enabled = info[1].As<Napi::Boolean>();
2135+
LOG_TO_STDERR_ENABLED = stderr_enabled;
2136+
LOG_TO_SYSLOG_ENABLED = syslog_enabled;
2137+
LOG("FS::set_log_config: " << DVAL(LOG_TO_STDERR_ENABLED) << DVAL(LOG_TO_SYSLOG_ENABLED));
21272138
return info.Env().Undefined();
21282139
}
21292140

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

22612272
exports_fs["dio_buffer_alloc"] = Napi::Function::New(env, dio_buffer_alloc);
22622273
exports_fs["set_debug_level"] = Napi::Function::New(env, set_debug_level);
2274+
exports_fs["set_log_config"] = Napi::Function::New(env, set_log_config);
22632275

22642276
exports["fs"] = exports_fs;
22652277
}

src/native/nb_native.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
'util/struct_buf.h',
6767
'util/struct_buf.cpp',
6868
'util/common.h',
69+
'util/common.cpp',
6970
'util/napi.h',
7071
'util/napi.cpp',
7172
'util/os.h',
@@ -104,6 +105,7 @@
104105
'util/buf.cpp',
105106
'util/buf.h',
106107
'util/common.h',
108+
'util/common.cpp',
107109
'util/compression.cpp',
108110
'util/compression.h',
109111
'util/gf2.h',

src/native/util/common.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "common.h"
2+
3+
namespace noobaa
4+
{
5+
bool LOG_TO_STDERR_ENABLED = true;
6+
bool LOG_TO_SYSLOG_ENABLED = false;
7+
}

src/native/util/common.h

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "backtrace.h"
2020
#include "os.h"
21+
#include "syslog.h"
2122

2223
namespace noobaa
2324
{
@@ -28,19 +29,46 @@ namespace noobaa
2829

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

31-
#define LOG(x) std::cerr << LOG_PREFIX() << x << std::endl
32+
extern bool LOG_TO_STDERR_ENABLED;
33+
extern bool LOG_TO_SYSLOG_ENABLED;
34+
35+
#define STD_LOG(x) \
36+
do { \
37+
std::cerr << x << std::endl; \
38+
} while (0)
39+
40+
#define SYS_LOG(x) \
41+
do { \
42+
const char* log_msg = x.c_str(); \
43+
int facility = LOG_LOCAL0; \
44+
int priority = 0; \
45+
::syslog(priority | facility, "%s", log_msg); \
46+
} while (0)
47+
48+
#define LOG(x) \
49+
do { \
50+
std::ostringstream oss; \
51+
oss << "" << LOG_PREFIX() << x; \
52+
std::string message = oss.str(); \
53+
if (LOG_TO_STDERR_ENABLED) { \
54+
STD_LOG(message); \
55+
} \
56+
if (LOG_TO_SYSLOG_ENABLED) { \
57+
SYS_LOG(message); \
58+
} \
59+
} while (0)
3260

3361
// to use DBG the module/file should use either DBG_INIT or DBG_INIT_VAR.
3462
#define DBG_INIT(level) static int __module_debug_var__ = level
3563
#define DBG_INIT_VAR(debug_var) static int& __module_debug_var__ = debug_var
3664
#define DBG_SET_LEVEL(level) __module_debug_var__ = level
3765
#define DBG_GET_LEVEL() (__module_debug_var__)
3866
#define DBG_VISIBLE(level) (level <= __module_debug_var__)
39-
#define DBG(level, x) \
40-
do { \
41-
if (DBG_VISIBLE(level)) { \
42-
LOG("[L" << level << "] " << x); \
43-
} \
67+
#define DBG(level, x) \
68+
do { \
69+
if (DBG_VISIBLE(level)) { \
70+
LOG("[L" << level << "] " << x); \
71+
} \
4472
} while (0)
4573
#define DBG0(x) DBG(0, x)
4674
#define DBG1(x) DBG(1, x)

src/sdk/nb.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ interface NativeFS {
942942

943943
dio_buffer_alloc(size: number): Buffer;
944944
set_debug_level(level: number);
945+
set_log_config(stderr_enabled: boolean, syslog_enabled: boolean,);
945946

946947
S_IFMT: number;
947948
S_IFDIR: number;

src/server/bg_services/semaphore_monitor.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class SemaphoreMonitor {
2727
}
2828

2929
async run_batch() {
30-
dbg.log1("semaphore_monitor: START");
3130
if (!this._can_run()) return;
3231
try {
3332
this.run_semaphore_monitor();

src/server/system_services/schemas/nsfs_config_schema.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,15 @@ const nsfs_node_config_schema = {
139139
ENDPOINT_PROCESS_TITLE: {
140140
type: 'string',
141141
description: 'This flag will set noobaa process title for letting GPFS to identify the noobaa endpoint processes.'
142-
}
142+
},
143+
LOG_TO_SYSLOG_ENABLED: {
144+
type: 'boolean',
145+
doc: 'This flag will enable syslog logging for the application.'
146+
},
147+
LOG_TO_STDERR_ENABLED: {
148+
type: 'boolean',
149+
doc: 'This flag will decide whether need to push logs to the console or not.'
150+
},
143151
}
144152
};
145153

src/test/unit_tests/jest_tests/test_nc_nsfs_config_schema_validation.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const nsfs_schema_utils = require('../../../manage_nsfs/nsfs_schema_utils');
66
const RpcError = require('../../../rpc/rpc_error');
7+
const config = require('../../../../config');
78

89
describe('schema validation NC NSFS config', () => {
910

@@ -251,6 +252,31 @@ describe('schema validation NC NSFS config', () => {
251252
nsfs_schema_utils.validate_nsfs_config_schema(config_data);
252253
});
253254
});
255+
256+
describe('skip/unskip schema check by config test', () => {
257+
258+
it('unskip schema check - config.LOG_TO_SYSLOG_ENABLED=false nsfs_config.LOG_TO_SYSLOG_ENABLED=bla - invalid config - should fail', () => {
259+
config.LOG_TO_SYSLOG_ENABLED = false;
260+
const config_data = {
261+
LOG_TO_SYSLOG_ENABLED: 'bla',
262+
};
263+
const reason = 'Test should have failed because of wrong type ' +
264+
'LOG_TO_SYSLOG_ENABLED must be boolean';
265+
const message = `must be boolean | {"type":"boolean"} | "/LOG_TO_SYSLOG_ENABLED"`;
266+
assert_validation(config_data, reason, message);
267+
});
268+
269+
it('unskip schema check - config.LOG_TO_STDERR_ENABLED=false nsfs_config.LOG_TO_STDERR_ENABLED=bla - invalid config - should fail', () => {
270+
config.LOG_TO_STDERR_ENABLED = false;
271+
const config_data = {
272+
LOG_TO_STDERR_ENABLED: 'bla',
273+
};
274+
const reason = 'Test should have failed because of wrong type ' +
275+
'LOG_TO_STDERR_ENABLED must be boolean';
276+
const message = `must be boolean | {"type":"boolean"} | "/LOG_TO_STDERR_ENABLED"`;
277+
assert_validation(config_data, reason, message);
278+
});
279+
});
254280
});
255281

256282
function assert_validation(config_to_validate, reason, basic_message) {

src/util/debug_module.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ util.inspect.defaultOptions.depth = 10;
4141
util.inspect.defaultOptions.colors = true;
4242
util.inspect.defaultOptions.breakLength = Infinity;
4343

44-
4544
//Detect our context, node/atom/browser
4645
//Different context requires different handling, for example rotating file steam usage or console wrapping
4746
let syslog;
@@ -379,7 +378,7 @@ class InternalDebugLogger {
379378
}
380379

381380
log_internal(msg_info) {
382-
if (syslog) {
381+
if (syslog && config.LOG_TO_SYSLOG_ENABLED) {
383382
// syslog path
384383
syslog(this._levels_to_syslog[msg_info.level], msg_info.message_syslog, config.DEBUG_FACILITY);
385384
} else if (this._log_file) {
@@ -389,7 +388,7 @@ class InternalDebugLogger {
389388
// This is also used in order to log to the console
390389
// browser workaround, don't use rotating file steam. Add timestamp and level
391390
const logfunc = LOG_FUNC_PER_LEVEL[msg_info.level] || 'log';
392-
if (this._log_console_silent) {
391+
if (this._log_console_silent || !config.LOG_TO_STDERR_ENABLED) {
393392
// noop
394393
} else if (console_wrapper) {
395394
process.stderr.write(msg_info.message_console + '\n');
@@ -586,10 +585,12 @@ if (console_wrapper) {
586585
console_wrapper.register_logger(conlogger);
587586
}
588587

589-
let native_log_level = LOG_LEVEL;
590-
if (process.env.NOOBAA_LOG_LEVEL) {
591-
native_log_level = dbg_conf.level;
592-
}
593-
if (Number(native_log_level)) {
594-
nb_native().fs.set_debug_level(Number(native_log_level));
588+
589+
function set_log_config() {
590+
const dbg_native_conf = debug_config.get_debug_config(process.env.NOOBAA_LOG_LEVEL);
591+
nb_native().fs.set_debug_level(dbg_native_conf.level);
592+
nb_native().fs.set_log_config(config.LOG_TO_STDERR_ENABLED, config.LOG_TO_SYSLOG_ENABLED);
595593
}
594+
595+
config.event_emitter.on("config_updated", set_log_config);
596+
set_log_config();

0 commit comments

Comments
 (0)