1/*
2Copyright (c) 2012, Broadcom Europe Ltd
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holder nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdarg.h>
31#include "containers/containers.h"
32#include "containers/core/containers_private.h"
33#include "containers/core/containers_logging.h"
34
35#ifndef ENABLE_CONTAINERS_STANDALONE
36# include "vcos.h"
37#endif
38
39#ifdef __ANDROID__
40#define LOG_TAG "ContainersCore"
41#include <cutils/log.h>
42#endif
43
44/* Default verbosity that will be inherited by containers */
45static uint32_t default_verbosity_mask = VC_CONTAINER_LOG_ALL;
46
47/* By default log everything that's not associated with a container context */
48static uint32_t verbosity_mask = VC_CONTAINER_LOG_ALL;
49
50void vc_container_log_set_default_verbosity(uint32_t mask)
51{
52 default_verbosity_mask = mask;
53}
54
55uint32_t vc_container_log_get_default_verbosity(void)
56{
57 return default_verbosity_mask;
58}
59
60void vc_container_log_set_verbosity(VC_CONTAINER_T *ctx, uint32_t mask)
61{
62 if(!ctx) verbosity_mask = mask;
63 else ctx->priv->verbosity = mask;
64}
65
66void vc_container_log(VC_CONTAINER_T *ctx, VC_CONTAINER_LOG_TYPE_T type, const char *format, ...)
67{
68 uint32_t verbosity = ctx ? ctx->priv->verbosity : verbosity_mask;
69 va_list args;
70
71 // Optimise out the call to vc_container_log_vargs etc. when it won't do anything.
72 if(!(type & verbosity)) return;
73
74 va_start( args, format );
75 vc_container_log_vargs(ctx, type, format, args);
76 va_end( args );
77}
78
79void vc_container_log_vargs(VC_CONTAINER_T *ctx, VC_CONTAINER_LOG_TYPE_T type, const char *format, va_list args)
80{
81 uint32_t verbosity = ctx ? ctx->priv->verbosity : verbosity_mask;
82
83 // If the verbosity is such that the type doesn't need logging quit now.
84 if(!(type & verbosity)) return;
85
86#ifdef __ANDROID__
87 {
88 // Default to Android's "verbose" level (doesn't usually come out)
89 android_LogPriority logLevel = ANDROID_LOG_VERBOSE;
90
91 // Where type suggest a higher level is required update logLevel.
92 // (Usually type contains only 1 bit as set by the LOG_DEBUG, LOG_ERROR or LOG_INFO macros)
93 if (type & VC_CONTAINER_LOG_ERROR)
94 logLevel = ANDROID_LOG_ERROR;
95 else if (type & VC_CONTAINER_LOG_INFO)
96 logLevel = ANDROID_LOG_INFO;
97 else if (type & VC_CONTAINER_LOG_DEBUG)
98 logLevel = ANDROID_LOG_DEBUG;
99
100 // Actually put the message out.
101 LOG_PRI_VA(logLevel, LOG_TAG, format, args);
102 }
103#else
104#ifndef ENABLE_CONTAINERS_STANDALONE
105 vcos_vlog(format, args);
106#else
107 vprintf(format, args); printf("\n");
108 fflush(0);
109#endif
110#endif
111}
112