cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

log.c (4121B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "config.h"
     21#include "log.h"
     22
     23#include <guacamole/client.h>
     24#include <guacamole/error.h>
     25
     26#include <stdarg.h>
     27#include <stdio.h>
     28#include <syslog.h>
     29#include <unistd.h>
     30
     31int guacd_log_level = GUAC_LOG_INFO;
     32
     33void vguacd_log(guac_client_log_level level, const char* format,
     34        va_list args) {
     35
     36    const char* priority_name;
     37    int priority;
     38
     39    char message[2048];
     40
     41    /* Don't bother if the log level is too high */
     42    if (level > guacd_log_level)
     43        return;
     44
     45    /* Copy log message into buffer */
     46    vsnprintf(message, sizeof(message), format, args);
     47
     48    /* Convert log level to syslog priority */
     49    switch (level) {
     50
     51        /* Error log level */
     52        case GUAC_LOG_ERROR:
     53            priority = LOG_ERR;
     54            priority_name = "ERROR";
     55            break;
     56
     57        /* Warning log level */
     58        case GUAC_LOG_WARNING:
     59            priority = LOG_WARNING;
     60            priority_name = "WARNING";
     61            break;
     62
     63        /* Informational log level */
     64        case GUAC_LOG_INFO:
     65            priority = LOG_INFO;
     66            priority_name = "INFO";
     67            break;
     68
     69        /* Debug log level */
     70        case GUAC_LOG_DEBUG:
     71            priority = LOG_DEBUG;
     72            priority_name = "DEBUG";
     73            break;
     74
     75        /* Trace log level */
     76        case GUAC_LOG_TRACE:
     77            priority = LOG_DEBUG;
     78            priority_name = "TRACE";
     79            break;
     80
     81        /* Any unknown/undefined log level */
     82        default:
     83            priority = LOG_INFO;
     84            priority_name = "UNKNOWN";
     85            break;
     86    }
     87
     88    /* Log to syslog */
     89    syslog(priority, "%s", message);
     90
     91    /* Log to STDERR */
     92    fprintf(stderr, GUACD_LOG_NAME "[%i]: %s:\t%s\n",
     93            getpid(), priority_name, message);
     94
     95}
     96
     97void guacd_log(guac_client_log_level level, const char* format, ...) {
     98    va_list args;
     99    va_start(args, format);
    100    vguacd_log(level, format, args);
    101    va_end(args);
    102}
    103
    104void guacd_client_log(guac_client* client, guac_client_log_level level,
    105        const char* format, va_list args) {
    106    vguacd_log(level, format, args);
    107}
    108
    109void guacd_log_guac_error(guac_client_log_level level, const char* message) {
    110
    111    if (guac_error != GUAC_STATUS_SUCCESS) {
    112
    113        /* If error message provided, include in log */
    114        if (guac_error_message != NULL)
    115            guacd_log(level, "%s: %s",
    116                    message,
    117                    guac_error_message);
    118
    119        /* Otherwise just log with standard status string */
    120        else
    121            guacd_log(level, "%s: %s",
    122                    message,
    123                    guac_status_string(guac_error));
    124
    125    }
    126
    127    /* Just log message if no status code */
    128    else
    129        guacd_log(level, "%s", message);
    130
    131}
    132
    133void guacd_log_handshake_failure() {
    134
    135    if (guac_error == GUAC_STATUS_CLOSED)
    136        guacd_log(GUAC_LOG_DEBUG,
    137                "Guacamole connection closed during handshake");
    138    else if (guac_error == GUAC_STATUS_PROTOCOL_ERROR)
    139        guacd_log(GUAC_LOG_ERROR,
    140                "Guacamole protocol violation. Perhaps the version of "
    141                "guacamole-client is incompatible with this version of "
    142                "guacd?");
    143    else
    144        guacd_log(GUAC_LOG_WARNING,
    145                "Guacamole handshake failed: %s",
    146                guac_status_string(guac_error));
    147
    148}
    149