0

I try to create logger from within the libuv server (simple). I followed the examples I barely found on the net.

I do manage to write to file but when I try to write to the log from some different place in the code I'm getting exception.

This is what I have:

const char* logfile = "wsserver.log";
char buf[] = "Server Started!\n";
uv_fs_t open_req;
uv_fs_t write_req;
uv_fs_t close_req;


int resultFD;


void open_cb(uv_fs_t* req);
void write_cb(uv_fs_t* req);
void close_cb(uv_fs_t* req);


void open_cb(uv_fs_t* req) {
 resultFD = req->result;
 const uv_buf_t buf1 = uv_buf_init(&buf, sizeof(buf));
 if (resultFD == -1) {
 fprintf(stderr, "Error opening file: %s.\n",
 uv_strerror(resultFD));
 }


 uv_fs_req_cleanup(req);
 uv_fs_write(loop, &write_req, resultFD, &buf1, sizeof(buf1), -1, write_cb);
}


void write_cb(uv_fs_t* req) {
 int result = req->result;


 if (result == -1) {
 fprintf(stderr, "Error writting data to file: %s.\n",
 uv_strerror(result));
 }


 uv_fs_req_cleanup(req);
 uv_fs_close(loop, &close_req, open_req.result, close_cb);
}


void close_cb(uv_fs_t* req) {
 int result = req->result;


 if (result == -1) {
 fprintf(stderr, "Error closing file: %s.\n",
 uv_strerror(result));
 }


 uv_fs_req_cleanup(req);
  
}


int main(int argc, char** argv)
{
 int port = 8011;
 loop = uv_default_loop();
 if (server_start(port))
 {
   
 return 1;
 }
 int r = uv_fs_open(loop, &open_req, logfile, O_CREAT | O_APPEND, 0644, open_cb);


 if (r) {
 fprintf(stderr, "Error opening file: %s.\n",
 uv_strerror(r));
 }
  
 uv_run(loop, UV_RUN_DEFAULT);
 return 0;
}

This code is working and it creates the file and append the "Server Started!\n" String. But the problem is when I try to write to the file from different place in the code.

For example: where I try to print the http request buffer ig getting exception in:

libuv_httpparser_ws.exe!uv__get_osfhandle(int fd) Line 174 C in file : handle-inl.h

Code:

void after_read(uv_stream_t* handle, ssize_t nread, const uv_buf_t * buf) {
 if (nread < 0) {
  
 if (buf->base) {
 free(buf->base);
 } 
 uv_close((uv_handle_t*)handle, on_close);
 return;
 }
 if (nread == 0) {
 free(buf->base);
 return;
 }
 
 _context* ctx = handle->data;
  
 if (ctx->request->handshake == 0) {
 //here you getting the request from the client 
  
 printf("buf->base %s\n", buf->base);
 uv_fs_write(loop, &write_req, resultFD, buf->base, sizeof(buf->base), -1, write_cb); //<---HERE I TRY TO WRITE TO THE FILE WITHOUT SUCCESS
 size_t np = http_parser_execute(ctx->parser, &settings, buf->base, nread);
  
 int _http_errno =ctx->parser->http_errno;
 const char * _errno = http_errno_description((enum http_errno)_http_errno);
 int _upgrade = ctx->parser->upgrade;
  
 free(buf->base);
 if (np != nread) {
 uv_shutdown_t* req;
 req = (uv_shutdown_t*)malloc(sizeof *req);
 uv_shutdown(req, handle, after_shutdown);
 }
 }
}

What am I doing wrong and how to make proper simple logger?

halfer
  • 19,824
  • 17
  • 99
  • 186
user63898
  • 29,839
  • 85
  • 272
  • 514

1 Answers1

0

You are freeing buf->base after calling uv_fs_write, but uv_fs_write won't copy the contents of the buffer, so the memory must remain valid until the write callback is called.

I'd suggest you use blocking writes for logging until it becomes a problem. Then maybe switch to using UDP for logging to a remote server.

saghul
  • 1,990
  • 1
  • 13
  • 15