Beispiel #1
0
//----------------------------------------------------------------------------------------------------------------------
// init_command_line_params
//----------------------------------------------------------------------------------------------------------------------
static bool init_command_line_params(int argc, char *argv[])
{
    VOGL_FUNC_TRACER

    command_line_params::parse_config parse_cfg;
    parse_cfg.m_single_minus_params = true;
    parse_cfg.m_double_minus_params = true;

    if (!g_command_line_params().parse(get_command_line_params(argc, argv),
                                     VOGL_ARRAY_SIZE(g_command_line_param_descs),
                                     g_command_line_param_descs, parse_cfg))
    {
        vogl_error_printf("%s: Failed parsing command line parameters!\n", VOGL_FUNCTION_NAME);
        return false;
    }

    if (!init_logfile())
        return false;

    if (g_command_line_params().get_value_as_bool("help") || g_command_line_params().get_value_as_bool("?"))
    {
        tool_print_help();
        return false;
    }

    return true;
}
Beispiel #2
0
/* static */
int daemon::start(const char *cwd, const char *pidfile /* = NULL */, const char *logfile /* = NULL */)
{
    int fdpid = -1;
    if (pidfile)
    {
        stored_pidfile = strdup(pidfile);
        if (NULL == stored_pidfile)
            LOGGER_ERROR_STR_EXIT(EXIT_FAILURE, "daemon::start, strdup");
        
        if ((fdpid = create_pidfile(pidfile)) < 0)
            exit(EXIT_FAILURE);
    }
    
    pid_t pid = fork();
    if (pid < 0)
    {
        logger::perror("daemon::start, fork");
        exit(EXIT_FAILURE);
    }
        
    if (pid > 0)
    {
        logger::log("daemon started (pid=%d)", pid);
        exit(EXIT_SUCCESS);
    }
   
    umask(0);
    
    if (0 > setsid())
    {
        logger::perror("setsid");
        exit(EXIT_FAILURE);
    }

    if (NULL != cwd && 0 > chdir("/"))
    {
        logger::perror("chdir");
        exit(EXIT_FAILURE);
    }

    reset_fds();
    
    init_logfile(logfile);
    
    pid = getpid();
    if (fdpid >= 0)
    {
        dprintf(fdpid, "%d", pid);
        ::close(fdpid);
    }
    
    logger::log("child process started (pid=%d)", pid);
    return 0;
}
Beispiel #3
0
void log_message(LogLevel ll, const char *fmt, ...) {

       
    if(ll <= LOG_OFF && ll >= current_log_level) {
    
        if (!init) {
            init_logfile();
        }
        // Choose output stream based on current log level

        //FILE *stream = (ll == LOG_INFO) ? stdout : stderr;

        // Log message level
        char *level_str = "";
        switch (ll) {
            case LOG_INFO : level_str = "[INFO] "; break;
            case LOG_WARN : level_str = "[WARN] "; break;
            case LOG_ERROR: level_str = "[ERROR] "; break;
            default : level_str = "[UNKNOWN] "; // Shouldn't happen 
        }
        
        EFI_STATUS efiStatus = root->Open(
            root, 
            &logfile,
            logfile_path,
            EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ,
            0);
    
        // Can't do anything, just drop out
        if (EFI_ERROR(efiStatus)) {
            return;    
        }
        
        logfile->SetPosition(logfile, offset);
   
        log_time();
        UINTN level_len = strlen(level_str);
        logfile->Write(logfile, &level_len, (CHAR16 *)level_str); 
        offset += level_len;
    
        // Log message
        va_list args;
        va_start(args, fmt);
    
        //static unsigned uint_to_hex_str(unsigned i, char *buf, int uppercase) {
        // static unsigned uint_to_str(unsigned i, char *buf) {
        char buffer[1024]; // Buffer overflow risk but oh well
        int buffer_index = 0;
        int len = strlen(fmt);
        
		for (int i = 0; i < len; i++) {
            for(; fmt[i] != '%' && i < len; i++) {
                buffer[buffer_index] = fmt[i];
                buffer_index++;
            }

            if (i >= len) {
                break;
            }
            i++;
            
			switch (fmt[i]) {
                case 'd' : {
                    int x = va_arg(args, int);
                    char int_buf[100];
                    unsigned len = uint_to_str((unsigned)x, int_buf);
                    memcpy(buffer + buffer_index, int_buf, len);
                    buffer_index += len;
                    break;
                }
                case 's' : {  
                    char *s = va_arg(args, char*);
                    int len = strlen(s);
                    memcpy(&buffer[buffer_index], s, len);
                    buffer_index += len;
                    break;
               }
				case 'l' : {
					if (i + 1 < len) {
						switch (fmt[i+1]) {
							case 's' : { // "long"/"wide" string
								CHAR16 *s = va_arg(args, CHAR16 *);
								UINTN len = StrLen(s);
								for (int i = 0; i < len; i++) {
									buffer[buffer_index++] = (char)(s[i]);
								}
								i++;
							}
						}
					}
				}
                case 'x' : {
                    unsigned x  = VA_ARG(args, unsigned);
                    char int_buf[100];
                    unsigned len = uint_to_hex_str(x, int_buf, 0);
                    memcpy(&buffer[buffer_index], int_buf, len);
                    buffer_index += len;
                    break;
                }
                case 'X' : {
                    unsigned x  = VA_ARG(args, unsigned);
                    char int_buf[100];
                    unsigned len = uint_to_hex_str(x, int_buf, 1);
                    memcpy(&buffer[buffer_index], int_buf, len);
                    buffer_index += len;
                    break;
                }
                default:
                    break;
            }
        }

        va_end(args);        
        
        offset += buffer_index;
    
        efiStatus = logfile->Write(logfile, (UINTN *)(&buffer_index), (CHAR16 *)buffer);
        logfile->Close(logfile); 
    } 
}