Example #1
0
/*
 * EXTERNAL ENTRY POINT
 *
 * Logs a fault, initializing the fault registry, if necessary.
 *
 * Returns the number of substitution parameters it was called with,
 * returning it as a negative number if the underlying log_eucafault_map()
 * call returned FALSE.
 */
int
log_eucafault (const char *fault_id, ...)
{
    va_list argv;
    char *token[2];
    char_map **m = NULL;
    int count = 0;
    int load = init_eucafaults (NULL);

    logprintfl (EUCATRACE, "init_eucafaults() returned %d\n", load);
    va_start (argv, fault_id);

    while ((token[count % 2] = va_arg (argv, char *)) != NULL) {
        ++count;
        if (! (count % 2)) {
            m = c_varmap_alloc (m, token[0], token[1]);
        }
    }
    va_end (argv);

    if (count % 2) {
        logprintfl (EUCAWARN, "log_eucafault() called with an odd (unmatched) number of substitution parameters: %d\n", count);
    }
    if (!log_eucafault_map (fault_id, (const char_map **)m)) {
        logprintfl (EUCADEBUG, "log_eucafault_map() returned FALSE inside log_eucafault()\n");
        count *= -1;
    }
    c_varmap_free (m);
    return count;
}
Example #2
0
int main (int argc, char **argv)
{
    log_params_set (EUCAWARN, 0, 0); // set log level
    log_prefix_set ("L "); // only print log level
    log_file_set (NULL); // log output goes to STANDARD_FILESTREAM

    char * component = NULL;
    int opt;
    while ((opt = getopt (argc, argv, "c:")) != -1) {
        switch (opt) {
        case 'c':
            component = optarg;
            break;
        case 'h':
        default:
            usage (argv[0]);
            return 1;
        }
    }

    if (argv[optind]==NULL) {
        logprintfl (EUCAERROR, "no fault ID is specified (try -h for usage)\n");
        return 1;
    }
    int ndigits = 0;
    for (char * c = argv[optind]; * c != '\0'; c++, ndigits++) {
        if ( ! isdigit (* c)) {
            logprintfl (EUCAERROR, "invalid fault ID (must be a number)\n");
            return 1;
        }
    }
    if (ndigits < 4) {
        logprintfl (EUCAERROR, "invalid fault ID (must be a 4-digit number)\n");
        return 1;
    }
    int nfaults = init_eucafaults (component);
    if (nfaults < 1) {
        logprintfl (EUCAERROR, "failed to locate fault information (is $EUCALYPTUS set?)\n");
        return 1;
    }

    // place variable-and-value pairs from command line into the map
    char_map ** m = NULL;
    for (int opt = optind + 1; opt < argc; opt++) {
        logprintfl (EUCADEBUG, "argv[opt]: %s\n", argv[opt]);
        if ((opt - optind + 1) % 2) {
            logprintfl (EUCADEBUG, "...now have two, calling c_varmap_alloc()\n");
            m = c_varmap_alloc (m, argv[opt - 1], argv[opt]);
        }
    }

    int ret = 0;
    if (log_eucafault_map (argv[optind], (const char_map **)m) == FALSE) {
        ret = 1;
    }
    if (m)
        c_varmap_free (m);
    
    return ret;
}
Example #3
0
//!
//! Main entry point of the application
//!
//! @param[in] argc the number of parameter passed on the command line
//! @param[in] argv the list of arguments
//!
//! @return EUCA_OK on success or EUCA_ERROR on failure.
//!
int main(int argc, char **argv)
{
    char *c_s1_sub = NULL;
    char *c_s2_sub = NULL;
    char *c_s3_sub = NULL;
    wchar_t *s1_sub = NULL;
    wchar_t *s2_sub = NULL;
    wchar_t *s3_sub = NULL;

    setlocale(LC_ALL, "en_US.UTF-8");

    m = varmap_alloc(NULL, L"color", L"brown");
    m = varmap_alloc(m, L"subject", L"føx");
    m = varmap_alloc(m, L"øbject", L"dog");

    printf("       nice string: %ls\n", s1);
    s1_sub = varsub(s1, (const wchar_map **)m);
    assert(s1_sub != NULL);
    printf("nice string subbed: %ls\n", s1_sub);
    EUCA_FREE(s1_sub);
    printf("       ugly string: %ls\n", s2);
    s2_sub = varsub(s2, (const wchar_map **)m);
    assert(s2_sub != NULL);
    printf("ugly string subbed: %ls\n", s2_sub);
    EUCA_FREE(s2_sub);
    printf(" unsubbable string: %ls\n", s3);
    assert(varsub(s3, (const wchar_map **)m) == NULL);

    varmap_free(m);

    printf("  sending null map: %ls\n", s3);    // Reuse s3
    s3_sub = varsub(s3, NULL);
    printf("returned from null: %ls\n", s3_sub);
    EUCA_FREE(s3_sub);

    // Now do it again, this time non-widechar
    c_m = c_varmap_alloc(NULL, "colorxxxxxx", "brown"); //! @todo This matches
    c_m = c_varmap_alloc(c_m, "subject", "fox");
    c_m = c_varmap_alloc(c_m, "object", "dog");

    printf("       nice string: %s\n", c_s1);
    c_s1_sub = c_varsub(c_s1, (const char_map **)c_m);
    printf("nice string subbed: %s\n", c_s1_sub);

    assert(c_s1_sub != NULL);
    EUCA_FREE(c_s1_sub);
    printf("       ugly string: %s\n", c_s2);
    c_s2_sub = c_varsub(c_s2, (const char_map **)c_m);
    assert(c_s2_sub != NULL);
    printf("ugly string subbed: %s\n", c_s2_sub);
    EUCA_FREE(c_s2_sub);

    printf(" unsubbable string: %s\n", c_s3);
    c_s3_sub = c_varsub(c_s3, (const char_map **)c_m);
    printf("   unsubbed string: %s\n", c_s3_sub);
    assert(!strcmp(c_s3, c_s3_sub));
    EUCA_FREE(c_s3_sub);

    c_varmap_free(c_m);

    printf("  sending null map: %s\n", c_s3);   // Reuse s3
    c_s3_sub = c_varsub(c_s3, NULL);
    printf("returned from null: %s\n", c_s3_sub);
    assert(!strcmp(c_s3, c_s3_sub));
    EUCA_FREE(c_s3_sub);
}
Example #4
0
/*
 * I am not an animal.
 */
int
main (int argc, char **argv)
{
    int dump = 0;
    int opt;

    //setlocale (LC_ALL, "en_US.utf-8");

    while ((opt = getopt (argc, argv, "d")) != -1) {
        switch (opt) {
        case 'd':
            dump++;
            break;
        default:
            usage (argv[0]);
            return 1;
        }
    }
    // NULL forces init_eucafaults()'s call to
    // initialize_faultlog() to guess at process name for creating
    // logfile.
    opt = init_eucafaults (NULL);

    logprintfl (EUCADEBUG, "init_eucafaults() returned %d\n", opt);

    if (optind < argc) {
        char_map **m = c_varmap_alloc (NULL, "daemon", "Balrog");
        m = c_varmap_alloc (m, "hostIp", "127.0.0.1");
        m = c_varmap_alloc (m, "brokerIp", "127.0.0.2");
        m = c_varmap_alloc (m, "endpointIp", "127.0.0.3");
        logprintfl (EUCATRACE, "argv[1st of %d]: %s\n", argc - optind, argv[optind]);
        log_eucafault_map (argv[optind], (const char_map **)m);
        c_varmap_free (m);

        // Now log to stdout for the remainder of the test.
        faultlog = stdout;

        // Reusing & abusing opt. :)
        opt = log_eucafault (argv[optind], "daemon", "Balrog",
                               "hostIp", "127.0.0.1",
                               "brokerIp", "127.0.0.2",
                               "endpointIp", "127.0.0.3",
                               "unmatched!", NULL);
        logprintfl (EUCADEBUG, "log_eucafault args returned: %d\n", opt);

        // This allows substitution-argument pairs for unit test to be
        // passed in on command line.
        m = NULL;
        for (opt = optind + 1; opt < argc; opt++) {
            logprintfl (EUCATRACE, "argv[opt]: %s\n", argv[opt]);
            if ((opt - optind + 1) % 2) {
                logprintfl (EUCATRACE, "...now have two, calling c_varmap_alloc()\n");
                m = c_varmap_alloc (m, argv[opt - 1], argv[opt]);
            }
        }
        if (m != NULL) {
            log_eucafault_map (argv[optind], (const char_map **)m);
            c_varmap_free (m);
        } else {
            log_eucafault_map (argv[optind], NULL);
        }
        log_eucafault (argv[optind], NULL); // Deliberately call w/NULL.
    }
    if (dump) {
        dump_eucafaults_db ();
    }
    if (optind >= argc) {
        usage (argv[0]);
        return 1;
    }
    return 0;
}