1/*
2 $License:
3   Copyright 2011 InvenSense, Inc.
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16  $
17 */
18/******************************************************************************
19 * $Id: log_linux.c 5629 2011-06-11 03:13:08Z mcaramello $
20 ******************************************************************************/
21
22/**
23 * @defgroup MPL_LOG
24 * @brief Logging facility for the MPL
25 *
26 * @{
27 *      @file     log.c
28 *      @brief    Core logging facility functions.
29 *
30 *
31**/
32
33#include <stdio.h>
34#include <string.h>
35#include "log.h"
36#include "mltypes.h"
37
38#define LOG_BUFFER_SIZE (256)
39
40#ifdef WIN32
41#define snprintf _snprintf
42#define vsnprintf _vsnprintf
43#endif
44
45int _MLPrintLog (int priority, const char* tag, const char* fmt, ...)
46{
47    va_list ap;
48    int result;
49
50    va_start(ap, fmt);
51    result = _MLPrintVaLog(priority,tag,fmt,ap);
52    va_end(ap);
53
54    return result;
55}
56
57int _MLPrintVaLog(int priority, const char* tag, const char* fmt, va_list args)
58{
59    int result;
60    char buf[LOG_BUFFER_SIZE];
61    char new_fmt[LOG_BUFFER_SIZE];
62    char priority_char;
63
64    if (NULL == fmt) {
65        fmt = "";
66    }
67
68    switch (priority) {
69    case MPL_LOG_UNKNOWN:
70        priority_char = 'U';
71        break;
72    case MPL_LOG_VERBOSE:
73        priority_char = 'V';
74        break;
75    case MPL_LOG_DEBUG:
76        priority_char = 'D';
77        break;
78    case MPL_LOG_INFO:
79        priority_char = 'I';
80        break;
81    case MPL_LOG_WARN:
82        priority_char = 'W';
83        break;
84    case MPL_LOG_ERROR:
85        priority_char = 'E';
86        break;
87    case MPL_LOG_SILENT:
88        priority_char = 'S';
89        break;
90    case MPL_LOG_DEFAULT:
91    default:
92        priority_char = 'D';
93        break;
94    };
95
96    result = snprintf(new_fmt, sizeof(new_fmt), "%c/%s:%s",
97                       priority_char, tag, fmt);
98    if (result <= 0) {
99        return INV_ERROR_LOG_MEMORY_ERROR;
100    }
101    result = vsnprintf(buf,sizeof(buf),new_fmt, args);
102    if (result <= 0) {
103        return INV_ERROR_LOG_OUTPUT_ERROR;
104    }
105
106    result = _MLWriteLog(buf, strlen(buf));
107    return INV_SUCCESS;
108}
109
110/**
111 * @}
112**/
113
114
115