Use neatvnc logging facilities

pull/142/head
Andri Yngvason 2022-06-25 16:14:18 +00:00
parent 43164f799d
commit 7c75c8919c
6 changed files with 53 additions and 83 deletions

View File

@ -1,28 +0,0 @@
/*
* Copyright (c) 2019 - 2020 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include <stdio.h>
#ifdef NDEBUG
#define log_debug(...)
#else
#define log_debug(...) fprintf(stderr, "DEBUG: " __VA_ARGS__)
#endif
#define log_error(...) fprintf(stderr, "ERROR: " __VA_ARGS__)
#define log_warning(...) fprintf(stderr, "Warning: " __VA_ARGS__)

View File

@ -22,8 +22,8 @@
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <aml.h> #include <aml.h>
#include <neatvnc.h>
#include "logging.h"
#include "data-control.h" #include "data-control.h"
struct receive_context { struct receive_context {
@ -79,13 +79,13 @@ static void receive_data(void* data,
int pipe_fd[2]; int pipe_fd[2];
if (pipe(pipe_fd) == -1) { if (pipe(pipe_fd) == -1) {
log_error("pipe() failed: %m\n"); nvnc_log(NVNC_LOG_ERROR, "pipe() failed: %m");
return; return;
} }
struct receive_context* ctx = calloc(1, sizeof(*ctx)); struct receive_context* ctx = calloc(1, sizeof(*ctx));
if (!ctx) { if (!ctx) {
log_error("OOM\n"); nvnc_log(NVNC_LOG_ERROR, "OOM");
close(pipe_fd[0]); close(pipe_fd[0]);
close(pipe_fd[1]); close(pipe_fd[1]);
return; return;
@ -102,7 +102,7 @@ static void receive_data(void* data,
if (!ctx->mem_fp) { if (!ctx->mem_fp) {
close(ctx->fd); close(ctx->fd);
free(ctx); free(ctx);
log_error("open_memstream() failed: %m\n"); nvnc_log(NVNC_LOG_ERROR, "open_memstream() failed: %m");
return; return;
} }
@ -199,7 +199,7 @@ data_control_source_send(void* data,
ret = write(fd, d, len); ret = write(fd, d, len);
if (ret < (int)len) if (ret < (int)len)
log_error("write from clipboard incomplete\n"); nvnc_log(NVNC_LOG_ERROR, "write from clipboard incomplete");
close(fd); close(fd);
} }
@ -227,7 +227,7 @@ static struct zwlr_data_control_source_v1* set_selection(struct data_control* se
struct zwlr_data_control_source_v1* selection; struct zwlr_data_control_source_v1* selection;
selection = zwlr_data_control_manager_v1_create_data_source(self->manager); selection = zwlr_data_control_manager_v1_create_data_source(self->manager);
if (selection == NULL) { if (selection == NULL) {
log_error("zwlr_data_control_manager_v1_create_data_source() failed\n"); nvnc_log(NVNC_LOG_ERROR, "zwlr_data_control_manager_v1_create_data_source() failed");
free(self->cb_data); free(self->cb_data);
self->cb_data = NULL; self->cb_data = NULL;
return NULL; return NULL;
@ -274,14 +274,14 @@ void data_control_destroy(struct data_control* self)
void data_control_to_clipboard(struct data_control* self, const char* text, size_t len) void data_control_to_clipboard(struct data_control* self, const char* text, size_t len)
{ {
if (!len) { if (!len) {
log_error("%s called with 0 length\n", __func__); nvnc_log(NVNC_LOG_ERROR, "%s called with 0 length", __func__);
return; return;
} }
free(self->cb_data); free(self->cb_data);
self->cb_data = malloc(len); self->cb_data = malloc(len);
if (!self->cb_data) { if (!self->cb_data) {
log_error("OOM: %m\n"); nvnc_log(NVNC_LOG_ERROR, "OOM: %m");
return; return;
} }

View File

@ -27,11 +27,11 @@
#include <xkbcommon/xkbcommon-keysyms.h> #include <xkbcommon/xkbcommon-keysyms.h>
#include <xkbcommon/xkbcommon.h> #include <xkbcommon/xkbcommon.h>
#include <wayland-client.h> #include <wayland-client.h>
#include <neatvnc.h>
#include "virtual-keyboard-unstable-v1.h" #include "virtual-keyboard-unstable-v1.h"
#include "keyboard.h" #include "keyboard.h"
#include "shm.h" #include "shm.h"
#include "logging.h"
#include "intset.h" #include "intset.h"
#define MAYBE_UNUSED __attribute__((unused)) #define MAYBE_UNUSED __attribute__((unused))
@ -143,7 +143,7 @@ static void keyboard__dump_entry(const struct keyboard* self,
bool is_pressed MAYBE_UNUSED = bool is_pressed MAYBE_UNUSED =
intset_is_set(&self->key_state, entry->code); intset_is_set(&self->key_state, entry->code);
log_debug("symbol=%s level=%d code=%s %s\n", sym_name, entry->level, nvnc_log(NVNC_LOG_DEBUG, "symbol=%s level=%d code=%s %s", sym_name, entry->level,
code_name, is_pressed ? "pressed" : "released"); code_name, is_pressed ? "pressed" : "released");
} }
@ -167,7 +167,7 @@ int keyboard_init(struct keyboard* self, const struct xkb_rule_names* rule_names
goto keymap_failure; goto keymap_failure;
if (xkb_keymap_num_layouts(self->keymap) > 1) if (xkb_keymap_num_layouts(self->keymap) > 1)
log_warning("Multiple keyboard layouts have been specified, but only one is supported.\n"); nvnc_log(NVNC_LOG_WARNING, "Multiple keyboard layouts have been specified, but only one is supported.");
self->state = xkb_state_new(self->keymap); self->state = xkb_state_new(self->keymap);
if (!self->state) if (!self->state)
@ -368,7 +368,7 @@ static void send_key_with_level(struct keyboard* self, xkb_keycode_t code,
XKB_STATE_MODS_LATCHED, XKB_STATE_MODS_LOCKED); XKB_STATE_MODS_LATCHED, XKB_STATE_MODS_LOCKED);
keyboard_send_mods(self); keyboard_send_mods(self);
log_debug("send key with level: old mods: %x, new mods: %x\n", nvnc_log(NVNC_LOG_DEBUG, "send key with level: old mods: %x, new mods: %x",
save.latched | save.locked | save.depressed, mods); save.latched | save.locked | save.depressed, mods);
send_key(self, code, is_pressed); send_key(self, code, is_pressed);
@ -397,7 +397,7 @@ void keyboard_feed(struct keyboard* self, xkb_keysym_t symbol, bool is_pressed)
struct table_entry* entry = keyboard_find_symbol(self, symbol); struct table_entry* entry = keyboard_find_symbol(self, symbol);
if (!entry) { if (!entry) {
char name[256]; char name[256];
log_error("Failed to look up keyboard symbol: %s\n", nvnc_log(NVNC_LOG_ERROR, "Failed to look up keyboard symbol: %s",
get_symbol_name(symbol, name, sizeof(name))); get_symbol_name(symbol, name, sizeof(name)));
return; return;
} }

View File

@ -43,7 +43,6 @@
#include "screencopy.h" #include "screencopy.h"
#include "data-control.h" #include "data-control.h"
#include "strlcpy.h" #include "strlcpy.h"
#include "logging.h"
#include "output.h" #include "output.h"
#include "pointer.h" #include "pointer.h"
#include "keyboard.h" #include "keyboard.h"
@ -226,7 +225,7 @@ static void registry_remove(void* data, struct wl_registry* registry,
output_destroy(out); output_destroy(out);
if (out == self->selected_output) { if (out == self->selected_output) {
log_error("Selected output went away. Exiting...\n"); nvnc_log(NVNC_LOG_ERROR, "Selected output went away. Exiting...");
wayvnc_exit(self); wayvnc_exit(self);
} }
@ -239,7 +238,7 @@ static void registry_remove(void* data, struct wl_registry* registry,
seat_destroy(seat); seat_destroy(seat);
if (seat == self->selected_seat) { if (seat == self->selected_seat) {
log_error("Selected seat went away. Exiting...\n"); nvnc_log(NVNC_LOG_ERROR, "Selected seat went away. Exiting...");
wayvnc_exit(self); wayvnc_exit(self);
} }
@ -361,14 +360,14 @@ static int init_wayland(struct wayvnc* self)
init_xdg_outputs(self); init_xdg_outputs(self);
if (!self->pointer_manager && !self->disable_input) { if (!self->pointer_manager && !self->disable_input) {
log_error("Virtual Pointer protocol not supported by compositor.\n"); nvnc_log(NVNC_LOG_ERROR, "Virtual Pointer protocol not supported by compositor.");
log_error("wayvnc may still work if started with --disable-input.\n"); nvnc_log(NVNC_LOG_ERROR, "wayvnc may still work if started with --disable-input.");
goto failure; goto failure;
} }
if (!self->keyboard_manager && !self->disable_input) { if (!self->keyboard_manager && !self->disable_input) {
log_error("Virtual Keyboard protocol not supported by compositor.\n"); nvnc_log(NVNC_LOG_ERROR, "Virtual Keyboard protocol not supported by compositor.");
log_error("wayvnc may still work if started with --disable-input.\n"); nvnc_log(NVNC_LOG_ERROR, "wayvnc may still work if started with --disable-input.");
goto failure; goto failure;
} }
@ -376,7 +375,7 @@ static int init_wayland(struct wayvnc* self)
wl_display_roundtrip(self->display); wl_display_roundtrip(self->display);
if (!self->screencopy.manager) { if (!self->screencopy.manager) {
log_error("Compositor doesn't support screencopy! Exiting.\n"); nvnc_log(NVNC_LOG_ERROR, "Compositor doesn't support screencopy! Exiting.");
goto failure; goto failure;
} }
@ -399,15 +398,15 @@ void on_wayland_event(void* obj)
if (wl_display_read_events(self->display) < 0) { if (wl_display_read_events(self->display) < 0) {
if (errno == EPIPE || errno == ECONNRESET) { if (errno == EPIPE || errno == ECONNRESET) {
log_error("Compositor has gone away. Exiting...\n"); nvnc_log(NVNC_LOG_ERROR, "Compositor has gone away. Exiting...");
wayvnc_exit(self); wayvnc_exit(self);
} else { } else {
log_error("Failed to read wayland events: %m\n"); nvnc_log(NVNC_LOG_ERROR, "Failed to read wayland events: %m");
} }
} }
if (wl_display_dispatch_pending(self->display) < 0) if (wl_display_dispatch_pending(self->display) < 0)
log_error("Failed to dispatch pending\n"); nvnc_log(NVNC_LOG_ERROR, "Failed to dispatch pending");
} }
void wayvnc_exit(struct wayvnc* self) void wayvnc_exit(struct wayvnc* self)
@ -525,7 +524,7 @@ int init_nvnc(struct wayvnc* self, const char* addr, uint16_t port, bool is_unix
{ {
self->nvnc = is_unix ? nvnc_open_unix(addr) : nvnc_open(addr, port); self->nvnc = is_unix ? nvnc_open_unix(addr) : nvnc_open(addr, port);
if (!self->nvnc) { if (!self->nvnc) {
log_error("Failed to bind to address\n"); nvnc_log(NVNC_LOG_ERROR, "Failed to bind to address");
return -1; return -1;
} }
@ -542,7 +541,7 @@ int init_nvnc(struct wayvnc* self, const char* addr, uint16_t port, bool is_unix
if (self->cfg.enable_auth && if (self->cfg.enable_auth &&
nvnc_enable_auth(self->nvnc, self->cfg.private_key_file, nvnc_enable_auth(self->nvnc, self->cfg.private_key_file,
self->cfg.certificate_file, on_auth, self) < 0) { self->cfg.certificate_file, on_auth, self) < 0) {
log_error("Failed to enable authentication\n"); nvnc_log(NVNC_LOG_ERROR, "Failed to enable authentication");
goto failure; goto failure;
} }
@ -560,7 +559,7 @@ int init_nvnc(struct wayvnc* self, const char* addr, uint16_t port, bool is_unix
create_placeholder_buffer(self->selected_output->width, create_placeholder_buffer(self->selected_output->width,
self->selected_output->height); self->selected_output->height);
if (!placeholder_fb) { if (!placeholder_fb) {
log_error("Failed to allocate a placeholder buffer\n"); nvnc_log(NVNC_LOG_ERROR, "Failed to allocate a placeholder buffer");
goto failure; goto failure;
} }
@ -584,7 +583,7 @@ int wayvnc_start_capture(struct wayvnc* self)
{ {
int rc = screencopy_start(&self->screencopy); int rc = screencopy_start(&self->screencopy);
if (rc < 0) { if (rc < 0) {
log_error("Failed to start capture. Exiting...\n"); nvnc_log(NVNC_LOG_ERROR, "Failed to start capture. Exiting...");
wayvnc_exit(self); wayvnc_exit(self);
} }
return rc; return rc;
@ -594,7 +593,7 @@ int wayvnc_start_capture_immediate(struct wayvnc* self)
{ {
int rc = screencopy_start_immediate(&self->screencopy); int rc = screencopy_start_immediate(&self->screencopy);
if (rc < 0) { if (rc < 0) {
log_error("Failed to start capture. Exiting...\n"); nvnc_log(NVNC_LOG_ERROR, "Failed to start capture. Exiting...");
wayvnc_exit(self); wayvnc_exit(self);
} }
return rc; return rc;
@ -606,7 +605,7 @@ void on_output_dimension_change(struct output* output)
struct wayvnc* self = output->userdata; struct wayvnc* self = output->userdata;
assert(self->selected_output == output); assert(self->selected_output == output);
log_debug("Output dimensions changed. Restarting frame capturer...\n"); nvnc_log(NVNC_LOG_DEBUG, "Output dimensions changed. Restarting frame capturer...");
screencopy_stop(&self->screencopy); screencopy_stop(&self->screencopy);
wayvnc_start_capture_immediate(self); wayvnc_start_capture_immediate(self);
@ -677,7 +676,7 @@ void on_capture_done(struct screencopy* sc)
case SCREENCOPY_IN_PROGRESS: case SCREENCOPY_IN_PROGRESS:
break; break;
case SCREENCOPY_FATAL: case SCREENCOPY_FATAL:
log_error("Fatal error while capturing. Exiting...\n"); nvnc_log(NVNC_LOG_ERROR, "Fatal error while capturing. Exiting...");
wayvnc_exit(self); wayvnc_exit(self);
break; break;
case SCREENCOPY_FAILED: case SCREENCOPY_FAILED:
@ -725,26 +724,26 @@ int check_cfg_sanity(struct cfg* cfg)
int rc = 0; int rc = 0;
if (!nvnc_has_auth()) { if (!nvnc_has_auth()) {
log_error("Authentication can't be enabled because it was not selected during build\n"); nvnc_log(NVNC_LOG_ERROR, "Authentication can't be enabled because it was not selected during build");
return -1; return -1;
} }
if (!cfg->certificate_file) { if (!cfg->certificate_file) {
log_error("Authentication enabled, but missing certificate_file\n"); nvnc_log(NVNC_LOG_ERROR, "Authentication enabled, but missing certificate_file");
rc = -1; rc = -1;
} }
if (!cfg->private_key_file) { if (!cfg->private_key_file) {
log_error("Authentication enabled, but missing private_key_file\n"); nvnc_log(NVNC_LOG_ERROR, "Authentication enabled, but missing private_key_file");
rc = -1; rc = -1;
} }
if (!cfg->username && !cfg->enable_pam) { if (!cfg->username && !cfg->enable_pam) {
log_error("Authentication enabled, but missing username\n"); nvnc_log(NVNC_LOG_ERROR, "Authentication enabled, but missing username");
rc = -1; rc = -1;
} }
if (!cfg->password && !cfg->enable_pam) { if (!cfg->password && !cfg->enable_pam) {
log_error("Authentication enabled, but missing password\n"); nvnc_log(NVNC_LOG_ERROR, "Authentication enabled, but missing password");
rc = -1; rc = -1;
} }
return rc; return rc;
@ -761,7 +760,7 @@ static void on_perf_tick(void* obj)
double area_avg = (double)self->damage_area_sum / (double)self->n_frames_captured; double area_avg = (double)self->damage_area_sum / (double)self->n_frames_captured;
double relative_area_avg = 100.0 * area_avg / total_area; double relative_area_avg = 100.0 * area_avg / total_area;
printf("Frames captured: %"PRIu32", average reported frame damage: %.1f %%\n", nvnc_log(NVNC_LOG_INFO, "Frames captured: %"PRIu32", average reported frame damage: %.1f %%",
self->n_frames_captured, relative_area_avg); self->n_frames_captured, relative_area_avg);
self->n_frames_captured = 0; self->n_frames_captured = 0;
@ -914,7 +913,7 @@ int main(int argc, char* argv[])
port = atoi(argv[optind + 1]); port = atoi(argv[optind + 1]);
if (seat_name && disable_input) { if (seat_name && disable_input) {
log_error("seat and disable-input are conflicting options\n"); nvnc_log(NVNC_LOG_ERROR, "seat and disable-input are conflicting options");
return 1; return 1;
} }
@ -922,10 +921,10 @@ int main(int argc, char* argv[])
int cfg_rc = cfg_load(&self.cfg, cfg_file); int cfg_rc = cfg_load(&self.cfg, cfg_file);
if (cfg_rc != 0 && (cfg_file || errno != ENOENT)) { if (cfg_rc != 0 && (cfg_file || errno != ENOENT)) {
if (cfg_rc > 0) { if (cfg_rc > 0) {
log_error("Failed to load config. Error on line %d\n", nvnc_log(NVNC_LOG_ERROR, "Failed to load config. Error on line %d",
cfg_rc); cfg_rc);
} else { } else {
log_error("Failed to load config. %m\n"); nvnc_log(NVNC_LOG_ERROR, "Failed to load config. %m");
} }
return 1; return 1;
@ -945,7 +944,7 @@ int main(int argc, char* argv[])
self.disable_input = disable_input; self.disable_input = disable_input;
if (init_wayland(&self) < 0) { if (init_wayland(&self) < 0) {
log_error("Failed to initialise wayland\n"); nvnc_log(NVNC_LOG_ERROR, "Failed to initialise wayland");
return 1; return 1;
} }
@ -953,13 +952,13 @@ int main(int argc, char* argv[])
if (output_name) { if (output_name) {
out = output_find_by_name(&self.outputs, output_name); out = output_find_by_name(&self.outputs, output_name);
if (!out) { if (!out) {
log_error("No such output\n"); nvnc_log(NVNC_LOG_ERROR, "No such output");
goto failure; goto failure;
} }
} else { } else {
out = output_first(&self.outputs); out = output_first(&self.outputs);
if (!out) { if (!out) {
log_error("No output found\n"); nvnc_log(NVNC_LOG_ERROR, "No output found");
goto failure; goto failure;
} }
} }
@ -968,13 +967,13 @@ int main(int argc, char* argv[])
if (seat_name) { if (seat_name) {
seat = seat_find_by_name(&self.seats, seat_name); seat = seat_find_by_name(&self.seats, seat_name);
if (!seat) { if (!seat) {
log_error("No such seat\n"); nvnc_log(NVNC_LOG_ERROR, "No such seat");
goto failure; goto failure;
} }
} else if (!self.disable_input) { } else if (!self.disable_input) {
seat = seat_first(&self.seats); seat = seat_first(&self.seats);
if (!seat) { if (!seat) {
log_error("No seat found\n"); nvnc_log(NVNC_LOG_ERROR, "No seat found");
goto failure; goto failure;
} }
} }
@ -999,7 +998,7 @@ int main(int argc, char* argv[])
}; };
if (keyboard_init(&self.keyboard_backend, &rule_names) < 0) { if (keyboard_init(&self.keyboard_backend, &rule_names) < 0) {
log_error("Failed to initialise keyboard\n"); nvnc_log(NVNC_LOG_ERROR, "Failed to initialise keyboard");
goto failure; goto failure;
} }
} }
@ -1025,7 +1024,7 @@ int main(int argc, char* argv[])
#ifdef ENABLE_SCREENCOPY_DMABUF #ifdef ENABLE_SCREENCOPY_DMABUF
if (init_render_node(&drm_fd) < 0) { if (init_render_node(&drm_fd) < 0) {
log_error("Failed to initialise DRM render node. No GPU acceleration will be available.\n"); nvnc_log(NVNC_LOG_ERROR, "Failed to initialise DRM render node. No GPU acceleration will be available.");
} }
#endif #endif
@ -1045,7 +1044,7 @@ int main(int argc, char* argv[])
screencopy_init(&self.screencopy); screencopy_init(&self.screencopy);
if (!self.screencopy.manager) { if (!self.screencopy.manager) {
log_error("screencopy is not supported by compositor\n"); nvnc_log(NVNC_LOG_ERROR, "screencopy is not supported by compositor");
goto capture_failure; goto capture_failure;
} }

View File

@ -21,10 +21,10 @@
#include <stdbool.h> #include <stdbool.h>
#include <wayland-client-protocol.h> #include <wayland-client-protocol.h>
#include <wayland-client.h> #include <wayland-client.h>
#include <neatvnc.h>
#include "output.h" #include "output.h"
#include "strlcpy.h" #include "strlcpy.h"
#include "logging.h"
#include "xdg-output-unstable-v1.h" #include "xdg-output-unstable-v1.h"
@ -198,7 +198,7 @@ struct output* output_new(struct wl_output* wl_output, uint32_t id)
{ {
struct output* output = calloc(1, sizeof(*output)); struct output* output = calloc(1, sizeof(*output));
if (!output) { if (!output) {
log_error("OOM\n"); nvnc_log(NVNC_LOG_ERROR, "OOM");
return NULL; return NULL;
} }

View File

@ -19,8 +19,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <security/pam_appl.h> #include <security/pam_appl.h>
#include <neatvnc.h>
#include "logging.h"
struct credentials { struct credentials {
const char* user; const char* user;
@ -62,19 +61,19 @@ bool pam_auth(const char* username, const char* password)
pam_handle_t* pamh; pam_handle_t* pamh;
int result = pam_start(service, username, &conv, &pamh); int result = pam_start(service, username, &conv, &pamh);
if (result != PAM_SUCCESS) { if (result != PAM_SUCCESS) {
log_error("ERROR: PAM start failed: %s\n", pam_strerror(pamh, result)); nvnc_log(NVNC_LOG_ERROR, "ERROR: PAM start failed: %s", pam_strerror(pamh, result));
return false; return false;
} }
result = pam_authenticate(pamh, PAM_SILENT|PAM_DISALLOW_NULL_AUTHTOK); result = pam_authenticate(pamh, PAM_SILENT|PAM_DISALLOW_NULL_AUTHTOK);
if (result != PAM_SUCCESS) { if (result != PAM_SUCCESS) {
log_error("PAM authenticate failed: %s\n", pam_strerror(pamh, result)); nvnc_log(NVNC_LOG_ERROR, "PAM authenticate failed: %s", pam_strerror(pamh, result));
goto error; goto error;
} }
result = pam_acct_mgmt(pamh, 0); result = pam_acct_mgmt(pamh, 0);
if (result != PAM_SUCCESS) { if (result != PAM_SUCCESS) {
log_error("PAM account management failed: %s\n", pam_strerror(pamh, result)); nvnc_log(NVNC_LOG_ERROR, "PAM account management failed: %s", pam_strerror(pamh, result));
goto error; goto error;
} }