Skip to content

Commit 92df8e6

Browse files
committed
src: externalize node.cc messages and print help, cleanups
1 parent 9fc6ec1 commit 92df8e6

4 files changed

Lines changed: 140 additions & 119 deletions

File tree

src/env.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void Environment::PrintSyncTrace() const {
2727
Local<v8::StackTrace> stack =
2828
StackTrace::CurrentStackTrace(isolate(), 10, StackTrace::kDetailed);
2929

30-
fprintf(stderr, "(node:%d) WARNING: Detected use of sync API\n", getpid());
30+
fprintf(stderr, STR_SYNC_WARNING, getpid());
3131

3232
for (int i = 0; i < stack->GetFrameCount() - 1; i++) {
3333
Local<StackFrame> stack_frame = stack->GetFrame(i);
@@ -38,10 +38,10 @@ void Environment::PrintSyncTrace() const {
3838

3939
if (stack_frame->IsEval()) {
4040
if (stack_frame->GetScriptId() == Message::kNoScriptIdInfo) {
41-
fprintf(stderr, " at [eval]:%i:%i\n", line_number, column);
41+
fprintf(stderr, " " STR_AT " [eval]:%i:%i\n", line_number, column);
4242
} else {
4343
fprintf(stderr,
44-
" at [eval] (%s:%i:%i)\n",
44+
" " STR_AT " [eval] (%s:%i:%i)\n",
4545
*script_name,
4646
line_number,
4747
column);
@@ -50,10 +50,11 @@ void Environment::PrintSyncTrace() const {
5050
}
5151

5252
if (fn_name_s.length() == 0) {
53-
fprintf(stderr, " at %s:%i:%i\n", *script_name, line_number, column);
53+
fprintf(stderr, " " STR_AT " %s:%i:%i\n",
54+
*script_name, line_number, column);
5455
} else {
5556
fprintf(stderr,
56-
" at %s (%s:%i:%i)\n",
57+
" " STR_AT " %s (%s:%i:%i)\n",
5758
*fn_name_s,
5859
*script_name,
5960
line_number,

src/messages/en/messages.h

Lines changed: 120 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,137 @@
11
#ifndef SRC_NODE_MESSAGES_SRC_H_
22
#define SRC_NODE_MESSAGES_SRC_H_
33

4+
// node --help output
5+
6+
#if HAVE_OPENSSL
7+
#define NODE_HELP_OPENSSL \
8+
" --tls-cipher-list=val use an alternative default TLS cipher list\n"
9+
#else // !HAVE_OPENSSL
10+
#define NODE_HELP_OPENSSL ""
11+
#endif // HAVE_OPENSSL
12+
13+
#if defined(NODE_HAVE_I18N_SUPPORT)
14+
#if !defined(NODE_HAVE_SMALL_ICU)
15+
#define NODE_HELP_I18N_SMALL_ICU \
16+
" note: linked-in ICU data is\n" \
17+
" present.\n"
18+
#else // defined(NODE_HAVE_SMALL_ICU)
19+
#define NODE_HELP_I18N_SMALL_ICU ""
20+
#endif // !defined(NODE_HAVE_SMALL_ICU)
21+
#define NODE_HELP_I18N \
22+
" --icu-data-dir=dir set ICU data load path to dir\n" \
23+
" (overrides NODE_ICU_DATA)\n" \
24+
NODE_HELP_I18N_SMALL_ICU
25+
26+
#define NODE_HELP_ICU_DATA \
27+
"NODE_ICU_DATA data path for ICU (Intl object) data\n" \
28+
" (will extend linked-in data)\n"
29+
30+
#else // !defined(NODE_HAVE_I18N_SUPPORT)
31+
#define NODE_HELP_ICU_DATA ""
32+
#define NODE_HELP_I18N ""
33+
#endif // defined(NODE_HAVE_I18N_SUPPORT)
34+
35+
// Determine the path separator
36+
#ifdef _WIN32
37+
#define NODE_HELP_SEP ";"
38+
#else
39+
#define NODE_HELP_SEP ":"
40+
#endif
41+
42+
#define STR_NODEHELP \
43+
"Usage: node [options] [ -e script | script.js ] [arguments] \n" \
44+
" node debug script.js [arguments] \n" \
45+
"\n" \
46+
"Options:\n" \
47+
" -v, --version print Node.js version\n" \
48+
" -e, --eval script evaluate script\n" \
49+
" -p, --print evaluate script and print result\n" \
50+
" -c, --check syntax check script without executing\n" \
51+
" -i, --interactive always enter the REPL even if stdin\n" \
52+
" does not appear to be a terminal\n" \
53+
" -r, --require module to preload (option can be repeated)\n" \
54+
" --no-deprecation silence deprecation warnings\n" \
55+
" --throw-deprecation throw an exception anytime a deprecated " \
56+
"function is used\n" \
57+
" --trace-deprecation show stack traces on deprecations\n" \
58+
" --trace-sync-io show stack trace when use of sync IO\n" \
59+
" is detected after the first tick\n" \
60+
" --track-heap-objects track heap object allocations for heap " \
61+
"snapshots\n" \
62+
" --prof-process process v8 profiler output generated\n" \
63+
" using --prof\n" \
64+
" --v8-options print v8 command line options\n" \
65+
NODE_HELP_OPENSSL \
66+
NODE_HELP_I18N \
67+
"\n" \
68+
"Environment variables:\n" \
69+
"NODE_PATH '" NODE_HELP_SEP "'-separated list of " \
70+
"directories\n" \
71+
" prefixed to the module search path.\n" \
72+
"NODE_DISABLE_COLORS set to 1 to disable colors in the REPL\n" \
73+
NODE_HELP_ICU_DATA \
74+
"NODE_REPL_HISTORY path to the persistent REPL history file\n" \
75+
"\n" \
76+
"Documentation can be found at https://nodejs.org/\n"
77+
78+
// Other messages ...
79+
480
#define NODE_MESSAGE_UNKNOWN "(Message Unknown)"
581

682
#define NODE_DEPRECATE_MESSAGE(what, alternate) \
783
what " is deprecated. Use " alternate " instead."
884

85+
#define STR_SYNC_WARNING "(node:%d) WARNING: Detected use of sync API\n"
86+
#define STR_AT "at"
87+
#define STR_OPENSSL_FIPS_FAIL "openssl fips failed: %s\n"
88+
#define STR_PROCESS_TICKDOMAIN_NONFUNCTION \
89+
"process._tickDomainCallback assigned to non-function\n"
90+
#define STR_RAW_ENCODING_REMOVED \
91+
"'raw' encoding (array of integers) has been removed. Use 'binary'.\n"
92+
#define STR_MODULE_VERSION_MISMATCH \
93+
"Module version mismatch. Expected %d, got %d."
94+
#define STR_BINDING "Binding %s"
95+
#define STR_NO_SUCH_MODULE "No such module: %s"
96+
#define STR_NO_SUCH_MODULE_LINKED "No such module was linked: %s"
97+
#define STR_DEBUGPORT_OUTOFRANGE "Debug port must be in range 1024 to 65535.\n"
98+
#define STR_REQUIRES_ARGUMENT "%s: %s requires an argument\n"
99+
#define STR_START_DEBUGGER_FAIL "Starting debugger on port %d failed\n"
100+
#define STR_START_DEBUGGER_AGENT "Starting debugger agent.\n"
101+
#define STR_BAD_OPTION "bad option"
102+
9103
// The messages used in src/*.cc
10104
// These are used only within the Node.js native source
11105
#define STR_CONVERT_ARGS_TO_UTF8_FAIL "Could not convert arguments to utf8."
12106
#define STR_OUTOFMEMORY "Out of memory"
13-
#define STR_CALLBACK_NOT_ASSIGNED \
14-
"init callback is not assigned to a function"
15-
#define STR_HOOKSSHOULDNOTBESET \
16-
"hooks should not be set while also enabled"
17-
#define STR_INITCALLBACK \
18-
"init callback must be a function"
19-
#define STR_INVALID_FILENAME \
20-
"filename must be a valid string"
21-
#define STR_INDEX_OUT_OF_RANGE \
22-
"out of range index"
23-
#define STR_ARGUMENT_BUFFER \
24-
"argument should be a Buffer"
25-
#define STR_ARGUMENT_STRING \
26-
"Argument must be a string"
27-
#define STR_ARGUMENT_ARRAYBUFFER \
28-
"argument is not an ArrayBuffer"
29-
#define STR_UNABLE_TO_SET_PROTOTYPE \
30-
"Unable to set Object prototype"
31-
#define STR_INVALID_HEX \
32-
"Invalid hex string"
33-
#define STR_OFFSET_OUTOFBOUNDS \
34-
"Offset is out of bounds"
35-
#define STR_LENGTH_OUTOFBOUNDS \
36-
"length out of bounds"
37-
#define STR_SANDBOX_OBJECT \
38-
"sandbox argument must be an object."
39-
#define STR_VMSCRIPT_AS_CONSTRUCTOR \
40-
"Must call vm.Script as a constructor."
41-
#define STR_CONTEXTIFIED_MUST_BE_OBJECT \
107+
#define STR_CALLBACK_NOT_ASSIGNED "init callback is not assigned to a function"
108+
#define STR_HOOKSSHOULDNOTBESET "hooks should not be set while also enabled"
109+
#define STR_INITCALLBACK "init callback must be a function"
110+
#define STR_INVALID_FILENAME "filename must be a valid string"
111+
#define STR_INDEX_OUT_OF_RANGE "out of range index"
112+
#define STR_ARGUMENT_BUFFER "argument should be a Buffer"
113+
#define STR_ARGUMENT_STRING "Argument must be a string"
114+
#define STR_ARGUMENT_ARRAYBUFFER "argument is not an ArrayBuffer"
115+
#define STR_UNABLE_TO_SET_PROTOTYPE "Unable to set Object prototype"
116+
#define STR_INVALID_HEX "Invalid hex string"
117+
#define STR_OFFSET_OUTOFBOUNDS "Offset is out of bounds"
118+
#define STR_LENGTH_OUTOFBOUNDS "length out of bounds"
119+
#define STR_SANDBOX_OBJECT "sandbox argument must be an object."
120+
#define STR_VMSCRIPT_AS_CONSTRUCTOR "Must call vm.Script as a constructor."
121+
#define STR_CONTEXTIFIED_MUST_BE_OBJECT \
42122
"contextifiedSandbox argument must be an object."
43-
#define STR_SANDBOX_ARGUMENT_CONVERSION \
123+
#define STR_SANDBOX_ARGUMENT_CONVERSION \
44124
"sandbox argument must have been converted to a context."
45-
#define STR_OPTIONS_OBJECT \
46-
"options must be an object"
47-
#define STR_TIMEOUT_POSITIVE \
48-
"timeout must be a positive number"
49-
#define STR_CANNOT_CALL_SCRIPT_METHODS \
125+
#define STR_OPTIONS_OBJECT "options must be an object"
126+
#define STR_TIMEOUT_POSITIVE "timeout must be a positive number"
127+
#define STR_CANNOT_CALL_SCRIPT_METHODS \
50128
"Script methods can only be called on script instances."
51-
#define STR_SCRIPT_EXECUTION_TIMEDOUT \
52-
"Script execution timed out."
53-
#define STR_NOT_STRING_BUFFER \
54-
"Not a string or buffer"
55-
#define STR_NOT_BUFFER \
56-
"Not a buffer"
57-
#define STR_SSLV2_METHODS_DISABLED \
58-
"SSLv2 methods disabled"
59-
#define STR_SSLV3_METHODS_DISABLED \
60-
"SSLv3 methods disabled"
61-
#define STR_UNKNOWN_METHOD \
62-
"Unknown method"
129+
#define STR_SCRIPT_EXECUTION_TIMEDOUT "Script execution timed out."
130+
#define STR_NOT_STRING_BUFFER "Not a string or buffer"
131+
#define STR_NOT_BUFFER "Not a buffer"
132+
#define STR_SSLV2_METHODS_DISABLED "SSLv2 methods disabled"
133+
#define STR_SSLV3_METHODS_DISABLED "SSLv3 methods disabled"
134+
#define STR_UNKNOWN_METHOD "Unknown method"
63135
#define STR_BAD_PARAMETER "Bad parameter"
64136
#define STR_PEM_READ_BIO "PEM_read_bio_PrivateKey"
65137
#define STR_CTX_USE_PRIVATEKEY "SSL_CTX_use_PrivateKey"
@@ -694,8 +766,7 @@
694766
"directory. This functionality is deprecated and will be removed soon.") \
695767
XX(MODULE_REQUIREREPL_DEPRECATED, "Module.requireRepl is deprecated.") \
696768
XX(NET_SERVER_CONNECTIONS_DEPRECATED, \
697-
NODE_DEPRECATE_MESSAGE("Server.connections", \
698-
"Server.getConnections")) \
769+
NODE_DEPRECATE_MESSAGE("Server.connections", "Server.getConnections")) \
699770
XX(NET_SERVER_CONNECTIONS_DEPRECATED_SET, \
700771
"Server.connections property is deprecated.") \
701772
XX(NET_SERVER_LISTENFD_DEPRECATED, \

src/node.cc

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ void SetupDomainUse(const FunctionCallbackInfo<Value>& args) {
10631063
process_object->Get(tick_callback_function_key).As<Function>();
10641064

10651065
if (!tick_callback_function->IsFunction()) {
1066-
fprintf(stderr, "process._tickDomainCallback assigned to non-function\n");
1066+
fprintf(stderr, STR_PROCESS_TICKDOMAIN_NONFUNCTION);
10671067
ABORT();
10681068
}
10691069

@@ -1417,8 +1417,7 @@ ssize_t DecodeBytes(Isolate* isolate,
14171417
HandleScope scope(isolate);
14181418

14191419
if (val->IsArray()) {
1420-
fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
1421-
"Use 'binary'.\n");
1420+
fprintf(stderr, STR_RAW_ENCODING_REMOVED);
14221421
UNREACHABLE();
14231422
return -1;
14241423
}
@@ -2277,7 +2276,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
22772276
char errmsg[1024];
22782277
snprintf(errmsg,
22792278
sizeof(errmsg),
2280-
"Module version mismatch. Expected %d, got %d.",
2279+
STR_MODULE_VERSION_MISMATCH,
22812280
NODE_MODULE_VERSION, mp->nm_version);
22822281

22832282
// NOTE: `mp` is allocated inside of the shared library's memory, calling
@@ -2404,7 +2403,7 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
24042403

24052404
// Append a string to process.moduleLoadList
24062405
char buf[1024];
2407-
snprintf(buf, sizeof(buf), "Binding %s", *module_v);
2406+
snprintf(buf, sizeof(buf), STR_BINDING, *module_v);
24082407

24092408
Local<Array> modules = env->module_load_list_array();
24102409
uint32_t l = modules->Length();
@@ -2436,7 +2435,7 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
24362435
char errmsg[1024];
24372436
snprintf(errmsg,
24382437
sizeof(errmsg),
2439-
"No such module: %s",
2438+
STR_NO_SUCH_MODULE,
24402439
*module_v);
24412440
return env->ThrowError(errmsg);
24422441
}
@@ -2462,7 +2461,7 @@ static void LinkedBinding(const FunctionCallbackInfo<Value>& args) {
24622461
char errmsg[1024];
24632462
snprintf(errmsg,
24642463
sizeof(errmsg),
2465-
"No such module was linked: %s",
2464+
STR_NO_SUCH_MODULE_LINKED,
24662465
*module_v);
24672466
return env->ThrowError(errmsg);
24682467
}
@@ -3268,7 +3267,7 @@ static bool ParseDebugOpt(const char* arg) {
32683267
if (port != nullptr) {
32693268
debug_port = atoi(port);
32703269
if (debug_port < 1024 || debug_port > 65535) {
3271-
fprintf(stderr, "Debug port must be in range 1024 to 65535.\n");
3270+
fprintf(stderr, STR_DEBUGPORT_OUTOFRANGE);
32723271
PrintHelp();
32733272
exit(12);
32743273
}
@@ -3278,57 +3277,7 @@ static bool ParseDebugOpt(const char* arg) {
32783277
}
32793278

32803279
static void PrintHelp() {
3281-
printf("Usage: node [options] [ -e script | script.js ] [arguments] \n"
3282-
" node debug script.js [arguments] \n"
3283-
"\n"
3284-
"Options:\n"
3285-
" -v, --version print Node.js version\n"
3286-
" -e, --eval script evaluate script\n"
3287-
" -p, --print evaluate script and print result\n"
3288-
" -c, --check syntax check script without executing\n"
3289-
" -i, --interactive always enter the REPL even if stdin\n"
3290-
" does not appear to be a terminal\n"
3291-
" -r, --require module to preload (option can be repeated)\n"
3292-
" --no-deprecation silence deprecation warnings\n"
3293-
" --throw-deprecation throw an exception anytime a deprecated "
3294-
"function is used\n"
3295-
" --trace-deprecation show stack traces on deprecations\n"
3296-
" --trace-sync-io show stack trace when use of sync IO\n"
3297-
" is detected after the first tick\n"
3298-
" --track-heap-objects track heap object allocations for heap "
3299-
"snapshots\n"
3300-
" --prof-process process v8 profiler output generated\n"
3301-
" using --prof\n"
3302-
" --v8-options print v8 command line options\n"
3303-
#if HAVE_OPENSSL
3304-
" --tls-cipher-list=val use an alternative default TLS cipher list\n"
3305-
#endif
3306-
#if defined(NODE_HAVE_I18N_SUPPORT)
3307-
" --icu-data-dir=dir set ICU data load path to dir\n"
3308-
" (overrides NODE_ICU_DATA)\n"
3309-
#if !defined(NODE_HAVE_SMALL_ICU)
3310-
" note: linked-in ICU data is\n"
3311-
" present.\n"
3312-
#endif
3313-
#endif
3314-
"\n"
3315-
"Environment variables:\n"
3316-
#ifdef _WIN32
3317-
"NODE_PATH ';'-separated list of directories\n"
3318-
#else
3319-
"NODE_PATH ':'-separated list of directories\n"
3320-
#endif
3321-
" prefixed to the module search path.\n"
3322-
"NODE_DISABLE_COLORS set to 1 to disable colors in the REPL\n"
3323-
#if defined(NODE_HAVE_I18N_SUPPORT)
3324-
"NODE_ICU_DATA data path for ICU (Intl object) data\n"
3325-
#if !defined(NODE_HAVE_SMALL_ICU)
3326-
" (will extend linked-in data)\n"
3327-
#endif
3328-
#endif
3329-
"NODE_REPL_HISTORY path to the persistent REPL history file\n"
3330-
"\n"
3331-
"Documentation can be found at https://nodejs.org/\n");
3280+
printf(STR_NODEHELP);
33323281
}
33333282

33343283

@@ -3396,7 +3345,7 @@ static void ParseArgs(int* argc,
33963345
args_consumed += 1;
33973346
eval_string = argv[index + 1];
33983347
if (eval_string == nullptr) {
3399-
fprintf(stderr, "%s: %s requires an argument\n", argv[0], arg);
3348+
fprintf(stderr, STR_REQUIRES_ARGUMENT, argv[0], arg);
34003349
exit(9);
34013350
}
34023351
} else if ((index + 1 < nargs) &&
@@ -3413,7 +3362,7 @@ static void ParseArgs(int* argc,
34133362
strcmp(arg, "-r") == 0) {
34143363
const char* module = argv[index + 1];
34153364
if (module == nullptr) {
3416-
fprintf(stderr, "%s: %s requires an argument\n", argv[0], arg);
3365+
fprintf(stderr, STR_REQUIRES_ARGUMENT, argv[0], arg);
34173366
exit(9);
34183367
}
34193368
args_consumed += 1;
@@ -3504,7 +3453,7 @@ static void StartDebug(Environment* env, bool wait) {
35043453
DispatchMessagesDebugAgentCallback);
35053454
debugger_running = env->debugger_agent()->Start(debug_port, wait);
35063455
if (debugger_running == false) {
3507-
fprintf(stderr, "Starting debugger on port %d failed\n", debug_port);
3456+
fprintf(stderr, STR_START_DEBUGGER_FAIL, debug_port);
35083457
fflush(stderr);
35093458
return;
35103459
}
@@ -3553,7 +3502,7 @@ static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle) {
35533502
} while (isolate == nullptr);
35543503

35553504
if (debugger_running == false) {
3556-
fprintf(stderr, "Starting debugger agent.\n");
3505+
fprintf(stderr, STR_START_DEBUGGER_AGENT);
35573506

35583507
HandleScope scope(isolate);
35593508
Environment* env = Environment::GetCurrent(isolate);
@@ -3914,7 +3863,7 @@ void Init(int* argc,
39143863

39153864
// Anything that's still in v8_argv is not a V8 or a node option.
39163865
for (int i = 1; i < v8_argc; i++) {
3917-
fprintf(stderr, "%s: bad option: %s\n", argv[0], v8_argv[i]);
3866+
fprintf(stderr, "%s: " STR_BAD_OPTION ": %s\n", argv[0], v8_argv[i]);
39183867
}
39193868
delete[] v8_argv;
39203869
v8_argv = nullptr;

0 commit comments

Comments
 (0)