Skip to content

Commit 422425a

Browse files
committed
feat: add methods to set and get Hamlib debug level, defaulting to NONE
1 parent cdf601c commit 422425a

File tree

6 files changed

+142
-12
lines changed

6 files changed

+142
-12
lines changed

index.d.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ type VFO = 'VFO-A' | 'VFO-B';
6666
*/
6767
type RadioMode = 'USB' | 'LSB' | 'FM' | 'PKTFM' | 'AM' | 'CW' | 'RTTY' | 'DIG' | string;
6868

69+
/**
70+
* Hamlib debug level type
71+
* Controls the verbosity of Hamlib library debug output
72+
*/
73+
type RigDebugLevel = 0 | 1 | 2 | 3 | 4 | 5;
74+
6975
/**
7076
* Memory channel data interface
7177
*/
@@ -276,6 +282,41 @@ declare class HamLib {
276282
*/
277283
static getHamlibVersion(): string;
278284

285+
/**
286+
* Set Hamlib debug level (affects all instances globally)
287+
* Controls the verbosity of Hamlib library debug output.
288+
* By default, debug level is set to 0 (NONE) to prevent unwanted output.
289+
*
290+
* @param level Debug level:
291+
* - 0 = NONE (no debug output) - default
292+
* - 1 = BUG (serious bug messages only)
293+
* - 2 = ERR (error messages)
294+
* - 3 = WARN (warning messages)
295+
* - 4 = VERBOSE (verbose messages)
296+
* - 5 = TRACE (trace messages - very detailed)
297+
* @static
298+
* @example
299+
* // Disable all debug output (default)
300+
* HamLib.setDebugLevel(0);
301+
*
302+
* // Enable verbose debugging for troubleshooting
303+
* HamLib.setDebugLevel(4);
304+
*
305+
* // Enable full trace debugging for development
306+
* HamLib.setDebugLevel(5);
307+
*/
308+
static setDebugLevel(level: RigDebugLevel): void;
309+
310+
/**
311+
* Get current Hamlib debug level
312+
* Note: This method is not supported by Hamlib API and will throw an error.
313+
* Applications should track the debug level they set using setDebugLevel().
314+
*
315+
* @static
316+
* @throws Always throws error as Hamlib doesn't provide API to query debug level
317+
*/
318+
static getDebugLevel(): never;
319+
279320
/**
280321
* Open connection to device
281322
* Must be called before other operations

lib/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,41 @@ class HamLib {
3939
return nativeModule.HamLib.getHamlibVersion();
4040
}
4141

42+
/**
43+
* Set Hamlib debug level (affects all instances globally)
44+
* @param {number} level - Debug level:
45+
* - 0 = NONE (no debug output)
46+
* - 1 = BUG (serious bug messages)
47+
* - 2 = ERR (error messages)
48+
* - 3 = WARN (warning messages)
49+
* - 4 = VERBOSE (verbose messages)
50+
* - 5 = TRACE (trace messages - very detailed)
51+
* @static
52+
* @example
53+
* // Disable all debug output (default)
54+
* HamLib.setDebugLevel(0);
55+
*
56+
* // Enable verbose debugging
57+
* HamLib.setDebugLevel(4);
58+
*
59+
* // Enable full trace debugging
60+
* HamLib.setDebugLevel(5);
61+
*/
62+
static setDebugLevel(level) {
63+
return nativeModule.HamLib.setDebugLevel(level);
64+
}
65+
66+
/**
67+
* Get current Hamlib debug level
68+
* Note: This method is not supported by Hamlib API and will throw an error.
69+
* Applications should track the debug level they set using setDebugLevel().
70+
* @static
71+
* @throws {Error} Always throws error as Hamlib doesn't provide API to query debug level
72+
*/
73+
static getDebugLevel() {
74+
return nativeModule.HamLib.getDebugLevel();
75+
}
76+
4277
/**
4378
* Open connection to the radio device
4479
* Must be called before other operations
@@ -1093,6 +1128,10 @@ class HamLib {
10931128
}
10941129
}
10951130

1131+
// Set debug level to NONE by default to prevent unwanted output
1132+
// Users can change this using HamLib.setDebugLevel() if needed
1133+
HamLib.setDebugLevel(0);
1134+
10961135
// Export for CommonJS
10971136
module.exports = { HamLib };
10981137
module.exports.HamLib = HamLib;

src/addon.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#include <napi.h>
22
#include "hamlib.h"
33
#include "decoder.h"
4+
#include <hamlib/rig.h>
45

56
Napi::Object Init(Napi::Env env, Napi::Object exports) {
7+
// Set Hamlib debug level to NONE by default to prevent unwanted output
8+
// Users can change this using HamLib.setDebugLevel() if needed
9+
rig_set_debug(RIG_DEBUG_NONE);
610

711
Napi::String name = Napi::String::New(env, "HamLib");
812
exports.Set(name, NodeHamLib::GetClass(env));

src/hamlib.cpp

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,12 +2214,9 @@ NodeHamLib::NodeHamLib(const Napi::CallbackInfo & info): ObjectWrap(info) {
22142214
std::string portStr = info[1].As<Napi::String>().Utf8Value();
22152215
strncpy(port_path, portStr.c_str(), HAMLIB_FILPATHLEN - 1);
22162216
port_path[HAMLIB_FILPATHLEN - 1] = '\0';
2217-
} else {
2218-
// If second argument exists but is not a string, treat it as debug level (backward compatibility)
2219-
rig_set_debug_level(RIG_DEBUG_NONE);
22202217
}
2221-
} else {
2222-
rig_set_debug_level(RIG_DEBUG_NONE);
2218+
// Note: Debug level is now controlled globally via HamLib.setDebugLevel()
2219+
// and set to RIG_DEBUG_NONE by default in addon initialization
22232220
}
22242221
//rig_model_t myrig_model;
22252222
// hamlib_port_t myport;
@@ -2241,19 +2238,21 @@ NodeHamLib::NodeHamLib(const Napi::CallbackInfo & info): ObjectWrap(info) {
22412238

22422239
// Check if port_path is a network address (contains colon)
22432240
is_network_rig = isNetworkAddress(port_path);
2244-
2241+
22452242
if (is_network_rig) {
22462243
// Use NETRIGCTL model for network connections
22472244
myrig_model = 2; // RIG_MODEL_NETRIGCTL
2248-
printf("Using network connection to %s\n", port_path);
2245+
// Network connection will be established on open()
22492246
}
22502247

22512248
my_rig = rig_init(myrig_model);
22522249
//int retcode = 0;
22532250
if (!my_rig) {
2254-
fprintf(stderr, "Unknown rig num: %d\n", myrig_model);
2255-
fprintf(stderr, "Please check riglist.h\n");
2256-
Napi::TypeError::New(env, "Unable to Init Rig").ThrowAsJavaScriptException();
2251+
// Create detailed error message
2252+
std::string errorMsg = "Unable to initialize rig (model: " +
2253+
std::to_string(myrig_model) +
2254+
"). Please check if the model number is valid.";
2255+
Napi::TypeError::New(env, errorMsg).ThrowAsJavaScriptException();
22572256
}
22582257

22592258
// Set port path and type based on connection type
@@ -3724,6 +3723,8 @@ Napi::Function NodeHamLib::GetClass(Napi::Env env) {
37243723
// Static methods
37253724
NodeHamLib::StaticMethod("getSupportedRigs", & NodeHamLib::GetSupportedRigs),
37263725
NodeHamLib::StaticMethod("getHamlibVersion", & NodeHamLib::GetHamlibVersion),
3726+
NodeHamLib::StaticMethod("setDebugLevel", & NodeHamLib::SetDebugLevel),
3727+
NodeHamLib::StaticMethod("getDebugLevel", & NodeHamLib::GetDebugLevel),
37273728
});
37283729
constructor = Napi::Persistent(ret);
37293730
constructor.SuppressDestruct();
@@ -3843,6 +3844,47 @@ Napi::Value NodeHamLib::GetHamlibVersion(const Napi::CallbackInfo& info) {
38433844
return Napi::String::New(env, hamlib_version2);
38443845
}
38453846

3847+
// Set Hamlib debug level
3848+
// Debug levels: 0=NONE, 1=BUG, 2=ERR, 3=WARN, 4=VERBOSE, 5=TRACE
3849+
Napi::Value NodeHamLib::SetDebugLevel(const Napi::CallbackInfo& info) {
3850+
Napi::Env env = info.Env();
3851+
3852+
if (info.Length() < 1 || !info[0].IsNumber()) {
3853+
Napi::TypeError::New(env, "Debug level (number) required").ThrowAsJavaScriptException();
3854+
return env.Undefined();
3855+
}
3856+
3857+
int level = info[0].As<Napi::Number>().Int32Value();
3858+
3859+
// Validate debug level (0-5)
3860+
if (level < 0 || level > 5) {
3861+
Napi::RangeError::New(env, "Debug level must be between 0 (NONE) and 5 (TRACE)").ThrowAsJavaScriptException();
3862+
return env.Undefined();
3863+
}
3864+
3865+
rig_set_debug((enum rig_debug_level_e)level);
3866+
3867+
return env.Undefined();
3868+
}
3869+
3870+
// Get current Hamlib debug level
3871+
Napi::Value NodeHamLib::GetDebugLevel(const Napi::CallbackInfo& info) {
3872+
Napi::Env env = info.Env();
3873+
3874+
// hamlib_get_debug() is not available in all versions, so we use a workaround
3875+
// by calling rig_debug_level which is a global variable in hamlib
3876+
// However, this is internal implementation, so we use rig_debug with RIG_DEBUG_NONE
3877+
// to get the current level. For now, we'll just return a note that this is not
3878+
// directly accessible. In practice, applications should track the level they set.
3879+
3880+
// Since there's no public API to query debug level in Hamlib,
3881+
// we'll document that users should track it themselves
3882+
Napi::Error::New(env,
3883+
"Getting debug level is not supported by Hamlib API. "
3884+
"Please track the debug level you set using setDebugLevel().").ThrowAsJavaScriptException();
3885+
return env.Undefined();
3886+
}
3887+
38463888
// Serial Port Configuration Methods
38473889

38483890
// Set serial configuration parameter (data_bits, stop_bits, parity, handshake, etc.)

src/hamlib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ class NodeHamLib : public Napi::ObjectWrap<NodeHamLib> {
168168
// Static method to get Hamlib version
169169
static Napi::Value GetHamlibVersion(const Napi::CallbackInfo&);
170170

171+
// Static methods to control debug level
172+
static Napi::Value SetDebugLevel(const Napi::CallbackInfo&);
173+
static Napi::Value GetDebugLevel(const Napi::CallbackInfo&);
174+
171175
static Napi::Function GetClass(Napi::Env);
172176

173177
static int freq_change_cb(RIG*, vfo_t, freq_t, void*);

test/test_loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ try {
123123
const totalMethods = instanceMethods.length + staticMethods.length;
124124

125125
test(`实例方法数量正确 (80个)`, () => instanceMethods.length === 80);
126-
test(`静态方法数量正确 (2个)`, () => staticMethods.length === 2);
127-
test(`总方法数量正确 (82个)`, () => totalMethods === 82);
126+
test(`静态方法数量正确 (4个)`, () => staticMethods.length === 4);
127+
test(`总方法数量正确 (84个)`, () => totalMethods === 84);
128128

129129
console.log(` 📊 实例方法: ${instanceMethods.length}个`);
130130
console.log(` 📊 静态方法: ${staticMethods.length}个`);

0 commit comments

Comments
 (0)