wilix-team / iohook

Node.js global keyboard and mouse listener.
https://wilix-team.github.io/iohook
MIT License
1.2k stars 291 forks source link

Don't show debug information when imported #219

Closed Richienb closed 3 years ago

Richienb commented 4 years ago

Expected Behavior

iohook should be imported without creating any noise in the terminal.

Current Behavior

It is logging this:

refresh_locale_list [476]: GetKeyboardLayoutList(0, NULL) found 2 layouts.
refresh_locale_list [493]: Received 2 locales.
load_input_helper [866]: refresh_locale_list() found 2 locale(s).

Possible Solution

Silently perform this in the background and only alert if failed.

Steps to Reproduce (for bugs)

require("iohook")

Context

I want to be able to silently import iohook without it spitting information in the terminal.

Your Environment

cc @Djiit

Richienb commented 4 years ago

@Djiit Ping!

Richienb commented 4 years ago

@Djiit Friendly ping!

Djiit commented 4 years ago

Hey there :-)

I'm sorry I don't have much free time these days. Maybe you could open a PR with a proposed solution ?

Richienb commented 4 years ago

Sure! I was going to do that initially except I've been getting dabbed on by the build requirements.

Djiit commented 4 years ago

The code is pretty old as we speak. I'm more able to maintain the JS aspects, as I'm not the original author :), so yeah, any help is appreciated ! Thanks!

Richienb commented 4 years ago

After some investigating, it appears that this line is the culprit:

https://github.com/wilix-team/iohook/blob/master/libuiohook/src/logger.c#L42

kwhat commented 4 years ago

Somewhere in the native interface for this library, probably iohook.cc, you can do something like this:

bool logger_proc(unsigned int level, const char *format, ...) {
    bool status = false;

    va_list args;
    switch (level) {
        #ifdef USE_DEBUG
        case LOG_LEVEL_DEBUG:
        case LOG_LEVEL_INFO:
            va_start(args, format);
            status = vfprintf(stdout, format, args) >= 0;
            va_end(args);
            break;
        #endif

        case LOG_LEVEL_WARN:
        case LOG_LEVEL_ERROR:
            va_start(args, format);
            // Alternatively, send the format and args to something other than stderr.
            status = vfprintf(stderr, format, args) >= 0;
            va_end(args);
            break;
    }

    return status;
}

...

hook_set_logger_proc(&logger_proc);
kwhat commented 4 years ago

I am not sure if node has a logging facility, but if it does, logger_proc should probably interface with it instead of std output. For example, libuiohook to Java logging through JNI: https://github.com/kwhat/jnativehook/blob/2.1/src/jni/jni_Logger.c#L97

Richienb commented 4 years ago

See: https://github.com/wilix-team/iohook/blob/cd1c5ae74c46d00e8739bee1dec66f58fb63e612/libuiohook/src/logger.c

kwhat commented 4 years ago

You shouldn't need to monkey around with libuiohook, only call hook_set_logger_proc with a new function pointer from iohook.cc.

Richienb commented 4 years ago

@kwhat I have no idea how to program in C++ but if we can somehow callback to the main javascript then it would be much easier to do logging.

kwhat commented 4 years ago

@Richienb just console.log?

Richienb commented 4 years ago

@kwhat console.log in C++? We need to communicate back to the javascript code using a callback that is provided with the data.

kwhat commented 4 years ago

Sorry, I am a bit of a novice at node outside of just using NPM to get and build stuff. It looks like iohook.cc already has a logger function setup, so the only thing that would need to be done would be to change that function to call a javascript function that does logging. This is effectively what is happening in the JNI example I provided earlier for the Java side of things using the built in java.util.logging.Logger. This library provides no javascript logging facility, so I am trying to figure out what we would call back to in logger_proc which is why I was asking about console.log. This function would just need to be updated to call out to the javascripts console methods to log instead of going out to stdout/stderr as it currently does. As I previously stated, I don't know much about node, but this seems to be the most basic method of logging in node. There are at least another 3 NPM logging libraries that do more advanced logging and the logger_proc method could easily interface with any number of those by either using some type of configuration or some yet to be created iohook javascript logging facility. This logging facility would need to have a javascript method that is called from the native code directly (logger_proc) and it would need to call a callback function to do the logging. There should also be a default callback function and a method of setting the current callback function. There maybe a way to do this from the native code directly and just skip the javascript part, but it would probably be easier and cleaner to just do it in javascript. I will have to do some more digging.

Richienb commented 4 years ago

@kwhat The easiest way to do this is to use a Nan Callback to pass on all the arguments logger_proc is provided with.

dk1-noida commented 3 years ago

To stop the logs coming from iohook you need simply pass false in the start function const ioHook = require('iohook'); ioHook.start(false);

ash0x0 commented 3 years ago

Will make sure this is in docs.

ash0x0 commented 3 years ago

Added to doc.