1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ACELITE_CONSOLE_LOG_IMPL_H
17 #define OHOS_ACELITE_CONSOLE_LOG_IMPL_H
18 
19 #include "acelite_config.h"
20 #include "js_config.h"
21 
22 #if IS_ENABLED(CONSOLE_LOG_OUTPUT)
23 
24 #include "jerryscript.h"
25 
26 namespace OHOS {
27 namespace ACELite {
28 /**
29  * The log level definations.
30  */
31 enum LogLevel : uint8_t {
32     LOG_LEVEL_NONE = 0,  // console.log
33     LOG_LEVEL_DEBUG = 1, // console.debug
34     LOG_LEVEL_INFO = 2,  // console.info
35     LOG_LEVEL_WARN = 3,  // console.warn
36     LOG_LEVEL_ERR = 4,   // console.error
37     LOG_LEVEL_FATAL = 5, // fatal, no console.fatal
38     LOG_LEVEL_TRACE = 6, // console.trace
39 };
40 
41 #ifdef TDD_ASSERTIONS
42 // add extra hanlder for TDD test cases
43 typedef void (*JSLogOutputExtraHandler)(OHOS::ACELite::LogLevel level, const char *logContent, size_t length);
44 void RegisterJSLogOutputHandler(JSLogOutputExtraHandler extraHandler);
45 #endif // TDD_ASSERTIONS
46 
47 /**
48  * @brief: the str to print out.
49  *
50  * @param logLevel the log level
51  * @param str the string to print out
52  */
53 void LogString(const LogLevel logLevel, const char * const str);
54 
55 /**
56  * @brief: Output given string into stdout or the log file.
57  *
58  * @param logLevel the log level
59  * @param str the string to print
60  * @param length the string's length
61  */
62 void Output(const LogLevel logLevel, const char * const str, const uint8_t length);
63 
64 /**
65  * @brief: Flush the output.
66  */
67 void FlushOutput();
68 
69 /**
70  * @fn ConsoleModule::LogNative()
71  *
72  * @brief The real implementation for all console log function, same implementation with the jerry's default IO.
73  *
74  * @param logLevel the log level, please refer to LogNative definations
75  * @param args the list of arguments
76  * @param length the length of arguments list
77  */
78 jerry_value_t LogNative(const LogLevel logLevel,
79                         const jerry_value_t *args,
80                         const jerry_length_t length);
81 
82 /**
83  * @brief: Print log level if needed.
84  *
85  * @param logLevel the log level
86  */
87 void LogOutLevel(const LogLevel logLevel);
88 
89 /**
90  * @brief: Log a single character to standard output or the log buffer.
91  * If log to buffer, will flush to file or stdout when '\n' or buffer is full.
92  *
93  * @param c the character to print
94  * @param logLevel the loglevel, need to output if c is '\n'
95  * @param endFlag the flag presents if this is the end of the console log, default is false
96  */
97 void LogChar(char c, const LogLevel logLevel, bool endFlag = false);
98 } // namespace ACELite
99 } // namespace OHOS
100 #endif // ENABLED(CONSOLE_LOG_OUTPUT)
101 #endif // OHOS_ACELITE_CONSOLE_LOG_IMPL_H
102