libhv icon indicating copy to clipboard operation
libhv copied to clipboard

How to disable log of libhv?

Open WorstCodeWay opened this issue 3 years ago • 20 comments

Hi, I'm using this great project in my project. Everything is perfect, but I want to disable the log from this project.

I mean close all logs totally, including console and log files. How to do that?

Thanks.

WorstCodeWay avatar Apr 07 '22 10:04 WorstCodeWay

hlog_disable

#include "hlog.h"

int main() {
    hlog_disable();
    ...
}

ithewei avatar Apr 07 '22 13:04 ithewei

hlog_disable

I tried something very likely by calling log_set_level(LOG_LEVEL_SILENT), but that doesn't work. I don't know if it is caused by the multiple instances of http_server_t and hv::TcpServer?

Besides, the log records the http server starting informaiton.

WorstCodeWay avatar Apr 10 '22 08:04 WorstCodeWay

You should call hlog_disable before first log print, such as main() entrance.

ithewei avatar Apr 11 '22 10:04 ithewei

You should call hlog_disable before first log print, such as main() entrance.

Oh, yes! That works. Thanks.

WorstCodeWay avatar Apr 11 '22 14:04 WorstCodeWay

Hi, sorry for re-opening this thread. I find new problems here. There are hv logs named libhv.20230814.log even though I have called hlog_disable(); or hlog_set_level(LOG_LEVEL_SILENT); at the very beginning within main() and Its' contents are:

2023-08-14 02:36:10.502 INFO  EventLoopThread started, tid=22742 [EventLoopThread.h:90:loop_thread]
2023-08-14 02:36:10.604 INFO  EventLoopThread started, tid=22746 [EventLoopThread.h:90:loop_thread]
2023-08-14 09:30:35.746 INFO  EventLoopThread started, tid=4946 [EventLoopThread.h:90:loop_thread]
2023-08-14 09:30:35.850 INFO  EventLoopThread started, tid=4949 [EventLoopThread.h:90:loop_thread]
2023-08-14 09:38:44.332 INFO  EventLoopThread started, tid=5259 [EventLoopThread.h:90:loop_thread]
2023-08-14 09:38:44.434 INFO  EventLoopThread started, tid=5262 [EventLoopThread.h:90:loop_thread]
2023-08-14 09:38:59.485 INFO  EventLoopThread stopped, tid=5259 [EventLoopThread.h:109:loop_thread]
2023-08-14 09:38:59.486 INFO  EventLoopThread stopped, tid=5262 [EventLoopThread.h:109:loop_thread]
2023-08-14 09:45:29.054 INFO  EventLoopThread started, tid=5359 [EventLoopThread.h:90:loop_thread]
2023-08-14 09:45:29.159 INFO  EventLoopThread started, tid=5363 [EventLoopThread.h:90:loop_thread]
2023-08-14 09:47:07.061 INFO  EventLoopThread stopped, tid=5506 [EventLoopThread.h:109:loop_thread]
2023-08-14 09:47:07.062 INFO  EventLoopThread stopped, tid=5509 [EventLoopThread.h:109:loop_thread]
2023-08-14 09:53:36.331 INFO  EventLoopThread stopped, tid=5856 [EventLoopThread.h:109:loop_thread]
2023-08-14 09:53:36.332 INFO  EventLoopThread stopped, tid=5859 [EventLoopThread.h:109:loop_thread]

and it seems refreshed every time after my application closed.

WorstCodeWay avatar Aug 14 '23 09:08 WorstCodeWay

Test like this:

#include "hv/hlog.h"

int main() {
    hlogi("before");
    hlog_disable();
    hlogi("after");
    return 0;
}

ithewei avatar Aug 15 '23 04:08 ithewei

Test like this:

#include "hv/hlog.h"

int main() {
    hlogi("before");
    hlog_disable();
    hlogi("after");
    return 0;
}

@ithewei Sorry for this seems no help. There is still a log file with following content:

2023-08-15 05:20:40.835 INFO  EventLoopThread stopped, tid=2335 [EventLoopThread.h:109:loop_thread]
2023-08-15 05:20:40.837 INFO  EventLoopThread stopped, tid=2338 [EventLoopThread.h:109:loop_thread]

And I find this log is recorded only after application closed. Besides, these logs are releated to a tcp-client onConnection callback, which seems a thread.

WorstCodeWay avatar Aug 15 '23 05:08 WorstCodeWay

What I mean is that if you test this way, the correct result should be that before is printed but after is not printed.

ithewei avatar Aug 15 '23 06:08 ithewei

Maybe you should see if calling hlog_set_level elsewhere in your program changes the log level

ithewei avatar Aug 15 '23 06:08 ithewei

@ithewei Ok, now I can make some conclusions. But we begin from these codes where logs sneaked from:

void loop_thread(const Functor& pre, const Functor& post) {
        hlogi("EventLoopThread started, tid=%ld", hv_gettid());
        setStatus(kStarted);

        if (pre) {
            loop_->queueInLoop([this, pre]{
                if (pre() != 0) {
                    loop_->stop();
                }
            });
        }

        loop_->run();
        assert(loop_->isStopped());

        if (post) {
            post();
        }

        setStatus(kStopped);
        hlogi("EventLoopThread stopped, tid=%ld", hv_gettid());
    }
  1. By adding hlog_disable(); into the very beginning of main can prevent EventLoopThread started ... logs into default log file
  2. By adding hlog_disable(); into the very beginning of TcpClient::onConnection callback can prevent EventLoopThread stopped ... logs into default log file
  3. Forgetting any codes of previous steps will case logs sneaked into log file respectively.

So, I add hlog_disable() into two places to prevent all logs from hvlib itself, However, I don't know why? Maybe, there is a bug ?

WorstCodeWay avatar Aug 15 '23 07:08 WorstCodeWay

No bug! Do you use other libraries in your application, is it possible that you use other libraries, which encapsulate libhv, and call hlog_set_level?

ithewei avatar Aug 15 '23 08:08 ithewei

Another way is to set the handler to change the log output mode,like this:

void custom_log_handler(int loglevel, const char* buf, int len) {
    // do nothing
}

hlog_set_handler(custom_log_handler);

ithewei avatar Aug 15 '23 08:08 ithewei

@ithewei

No bug! Do you use other libraries in your application, is it possible that you use other libraries, which encapsulate libhv, and call hlog_set_level?

Not really. My third-party dependencies are qt, spdlog, opencv, pcl, hvlib, that's all.

Another way is to set the handler to change the log output mode,like this:

It's more clear, I'll try this.

WorstCodeWay avatar Aug 15 '23 09:08 WorstCodeWay

@ithewei

Another way is to set the handler to change the log output mode,like this:

void custom_log_handler(int loglevel, const char* buf, int len) {
    // do nothing
}

hlog_set_handler(custom_log_handler);

Unlucky, no helps either when putting it into beginning of main

WorstCodeWay avatar Aug 15 '23 09:08 WorstCodeWay

And new found! Every time there is a new TcpClient, I should add hlog_disable at beginning of onConnection callback to prevent logs.

WorstCodeWay avatar Aug 15 '23 09:08 WorstCodeWay

@ithewei

Another way is to set the handler to change the log output mode,like this:

void custom_log_handler(int loglevel, const char* buf, int len) {
    // do nothing
}

hlog_set_handler(custom_log_handler);

Unlucky, no helps either when putting it into beginning of main

This is impossible, unless you call hlog_destory halfway, are you sure you haven't called hlog_set_level and hlog_destory in your program?

ithewei avatar Aug 15 '23 09:08 ithewei

@ithewei

Another way is to set the handler to change the log output mode,like this:

void custom_log_handler(int loglevel, const char* buf, int len) {
    // do nothing
}

hlog_set_handler(custom_log_handler);

Unlucky, no helps either when putting it into beginning of main

This is impossible, unless you call hlog_destory halfway, are you sure you haven't called hlog_set_level and hlog_destory in your program?

I can confirm it by search all matches of hlog_set_level and hlog_destory in my codes, but find none.

Please NOTE FOLLOWING

Maybe this should be helpful from hvlib source code in file hlog.c:

logger_t* hv_default_logger() {
    if (s_logger == NULL) {
        s_logger = logger_create();
        atexit(hv_destroy_default_logger);
    }
    return s_logger;
}

Here, atexit is interesting.

/* Register a function to be called when `exit' is called. / extern int atexit (void (__func) (void)) __THROW __nonnull ((1));

So, when there is a tcp-client connection, and after application(Qt application) is terminated. Can the client connected thread exit after the main thread? If so, then the main thread will call exit (I don't know)? and then the client thread call hlogi, Another Creation!

WorstCodeWay avatar Aug 15 '23 09:08 WorstCodeWay

1、You can explicitly delete TcpClient before app exit; 2、remove the code line hlogi("EventLoopThread stopped... directly.

ithewei avatar Aug 17 '23 11:08 ithewei

1、You can explicitly delete TcpClient before app exit; 2、remove the code line hlogi("EventLoopThread stopped... directly.

Yes, it's better.

WorstCodeWay avatar Aug 21 '23 08:08 WorstCodeWay

I encounted this problem too

lovelypp777 avatar Mar 20 '24 05:03 lovelypp777