void LoggerDLT::messagev(const MessageOptions &options,
                         const char *format,
                         va_list args)
{
    // always to parent first (usually stdout):
    // if the parent is a LogRedirect instance, then
    // it'll flush its own output first, which ensures
    // that the new output comes later (as desired)
    {
        va_list argscopy;
        va_copy(argscopy, args);
        m_parentLogger.messagev(options, format, argscopy);
        va_end(argscopy);
    }

    DltContextData log;
    if (!(options.m_flags & MessageOptions::ALREADY_LOGGED) &&
        dlt_user_log_write_start((DltContext *)m_dltContext, &log, SyncEvoLevel2DLTLevel(options.m_level)) > 0) {
        std::string buffer = StringPrintfV(format, args);
        // Avoid almost empty messages. They are triggered by
        // SyncEvolution to format the INFO output and don't add any
        // valuable information to the DLT log.
        if (!buffer.empty() &&
            buffer != "\n") {
            dlt_user_log_write_string(&log, buffer.c_str());
            dlt_user_log_write_finish(&log);
        }
    }
}
int LoggerDLT::getCurrentDLTLogLevel()
{
    if (LoggerDLTInstance) {
        for (int level = DLT_LOG_VERBOSE;
             level > DLT_LOG_DEFAULT;
             --level) {
            DltContextData log;
            // Emulates DLT_LOG(): logging active if dlt_user_log_write_start() returns something > 0.
            // Otherwise discard the DltContextData without doing anything.
            if (dlt_user_log_write_start((DltContext *)LoggerDLTInstance->m_dltContext, &log, (DltLogLevelType)level) > 0) {
                return level;
            }
        }
    }
    return DLT_LOG_DEFAULT;
}
Exemplo n.º 3
0
/**
 * Main function of tool.
 */
int main(int argc, char* argv[])
{
    int vflag = 0;
    int gflag = 0;
    int aflag = 0;
    char *dvalue = 0;
    char *fvalue = 0;
    char *nvalue = 0;
    char *message = 0;

    int index;
    int c;
    char *text;
	int num,maxnum;
	int delay;

    opterr = 0;

    while ((c = getopt (argc, argv, "vgad:f:n:")) != -1)
    {
        switch (c)
        {
        case 'v':
        {
            vflag = 1;
            break;
        }
        case 'g':
        {
            gflag = 1;
            break;
        }
        case 'a':
        {
            aflag = 1;
            break;
        }
        case 'd':
        {
            dvalue = optarg;
            break;
        }
        case 'f':
        {
            fvalue = optarg;
            break;
        }
        case 'n':
        {
            nvalue = optarg;
            break;
        }
        case '?':
        {
            if (optopt == 'd' || optopt == 'f' || optopt == 'n')
            {
                fprintf (stderr, "Option -%c requires an argument.\n", optopt);
            }
            else if (isprint (optopt))
            {
                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
            }
            else
            {
                fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
            }

            /* unknown or wrong option used, show usage information and terminate */
            usage();
            return -1;
        }
        default:
        {
            abort ();
        }
        }
    }

    for (index = optind; index < argc; index++)
    {
        message = argv[index];
    }

    if (message == 0)
    {
        /* no message, show usage and terminate */
        fprintf(stderr,"ERROR: No message selected\n");
        usage();
        return -1;
    }

    if (fvalue)
    {
        /* DLT is intialised automatically, except another output target will be used */
        if (dlt_init_file(fvalue)<0) /* log to file */
        {
            return -1;
        }
    }

    dlt_register_app("LOG","Test Application for Logging");

    dlt_register_context(&mycontext,"TEST","Test Context for Logging");

    dlt_register_injection_callback(&mycontext, 0xFFF, dlt_user_injection_callback);

    text = message;

    if (gflag)
    {
        dlt_nonverbose_mode();
    }

    if (aflag)
    {
        dlt_enable_local_print();
    }

    if (nvalue)
    {
        maxnum = atoi(nvalue);
    }
    else
    {
		maxnum = 10;
    }

    if (dvalue)
    {
        delay = atoi(dvalue) * 1000;
    }
    else
    {
        delay = 500 * 1000;
    }

	if (gflag)
    {
        /* DLT messages to test Fibex non-verbose description: dlt-example-non-verbose.xml */
		if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,10)>0)
		{
			dlt_user_log_write_finish(&mycontextdata);
		}
		if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,11)>0)
		{
			dlt_user_log_write_uint16(&mycontextdata,1011);
			dlt_user_log_write_finish(&mycontextdata);
		}
		if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,12)>0)
		{
			dlt_user_log_write_uint32(&mycontextdata,1012);
			dlt_user_log_write_uint32(&mycontextdata,1013);
			dlt_user_log_write_finish(&mycontextdata);
		}
		if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,13)>0)
		{
			dlt_user_log_write_uint8(&mycontextdata,123);
			dlt_user_log_write_float32(&mycontextdata,1.12);
			dlt_user_log_write_finish(&mycontextdata);
		}
		if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,14)>0)
		{
			dlt_user_log_write_string(&mycontextdata,"DEAD BEEF");
			dlt_user_log_write_finish(&mycontextdata);
		}
    }

    for (num=0;num<maxnum;num++)
    {
        printf("Send %d %s\n",num,text);

        if (gflag)
        {
            /* Non-verbose mode */
            if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_WARN,num)>0)
            {
                dlt_user_log_write_int(&mycontextdata,num);
                dlt_user_log_write_string(&mycontextdata,text);
                dlt_user_log_write_finish(&mycontextdata);
            }
        }
        else
        {
            /* Verbose mode */
            if (dlt_user_log_write_start(&mycontext,&mycontextdata,DLT_LOG_WARN)>0)
            {
                dlt_user_log_write_int(&mycontextdata,num);
                dlt_user_log_write_string(&mycontextdata,text);
                dlt_user_log_write_finish(&mycontextdata);
            }
        }

        if (delay>0)
        {
            usleep(delay);
        }
    }

    dlt_unregister_context(&mycontext);

    dlt_unregister_app();

    dlt_free();

    return 0;
}