1# HiLog Development<a name="EN-US_TOPIC_0000001185655870"></a> 2 3## Overview<a name="section8154107175019"></a> 4 5HiLog is the log system of OpenHarmony that provides logging for the system framework, services, and applications to record information on user operations and system running status. 6 7This development guide is applicable to standard-system devices \(reference memory ≥ 128 MB\). 8 9## Available APIs<a name="section6748124155012"></a> 10 11**Table 1** List of C++ and C APIs 12 13<a name="table94501354193619"></a> 14<table><thead align="left"><tr id="row954285483614"><th class="cellrowborder" valign="top" width="18.258174182581744%" id="mcps1.2.4.1.1"><p id="p16542254203611"><a name="p16542254203611"></a><a name="p16542254203611"></a><strong id="b17542185414366"><a name="b17542185414366"></a><a name="b17542185414366"></a>C++</strong></p> 15</th> 16<th class="cellrowborder" valign="top" width="43.39566043395661%" id="mcps1.2.4.1.2"> </th> 17<th class="cellrowborder" valign="top" width="38.34616538346165%" id="mcps1.2.4.1.3"><p id="p185431554163619"><a name="p185431554163619"></a><a name="p185431554163619"></a><strong id="b175431054123617"><a name="b175431054123617"></a><a name="b175431054123617"></a>C</strong></p> 18</th> 19</tr> 20</thead> 21<tbody><tr id="row954305418361"><td class="cellrowborder" valign="top" width="18.258174182581744%" headers="mcps1.2.4.1.1 "><p id="p3543754183618"><a name="p3543754183618"></a><a name="p3543754183618"></a><strong id="b2050619101839"><a name="b2050619101839"></a><a name="b2050619101839"></a>Class</strong></p> 22</td> 23<td class="cellrowborder" valign="top" width="43.39566043395661%" headers="mcps1.2.4.1.2 "><p id="p10544105453610"><a name="p10544105453610"></a><a name="p10544105453610"></a><strong id="b185619133317"><a name="b185619133317"></a><a name="b185619133317"></a>API</strong></p> 24</td> 25<td class="cellrowborder" valign="top" width="38.34616538346165%" headers="mcps1.2.4.1.3 "><p id="p17969916192614"><a name="p17969916192614"></a><a name="p17969916192614"></a><strong id="b1316016214312"><a name="b1316016214312"></a><a name="b1316016214312"></a>API/Macro</strong></p> 26</td> 27</tr> 28<tr id="row8544115410361"><td class="cellrowborder" rowspan="7" valign="top" width="18.258174182581744%" headers="mcps1.2.4.1.1 "><p id="p16544154183615"><a name="p16544154183615"></a><a name="p16544154183615"></a>HiLog</p> 29<p id="p956013011212"><a name="p956013011212"></a><a name="p956013011212"></a></p> 30<p id="p1356013309127"><a name="p1356013309127"></a><a name="p1356013309127"></a></p> 31<p id="p756011303129"><a name="p756011303129"></a><a name="p756011303129"></a></p> 32<p id="p135601330191211"><a name="p135601330191211"></a><a name="p135601330191211"></a></p> 33<p id="p756183091215"><a name="p756183091215"></a><a name="p756183091215"></a></p> 34<p id="p1956115302128"><a name="p1956115302128"></a><a name="p1956115302128"></a></p> 35</td> 36<td class="cellrowborder" valign="top" width="43.39566043395661%" headers="mcps1.2.4.1.2 "><p id="p1326662513016"><a name="p1326662513016"></a><a name="p1326662513016"></a>int Debug(const HiLogLabel &label, const char *fmt, ...)</p> 37</td> 38<td class="cellrowborder" valign="top" width="38.34616538346165%" headers="mcps1.2.4.1.3 "><p id="p18697644105619"><a name="p18697644105619"></a><a name="p18697644105619"></a>HILOG_DEBUG(type, ...)</p> 39</td> 40</tr> 41<tr id="row754495418366"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p86951544155610"><a name="p86951544155610"></a><a name="p86951544155610"></a>int Info(const HiLogLabel &label, const char *fmt, ...)</p> 42</td> 43<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p96931344145615"><a name="p96931344145615"></a><a name="p96931344145615"></a>HILOG_INFO(type, ...)</p> 44</td> 45</tr> 46<tr id="row95441954113614"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1769224485619"><a name="p1769224485619"></a><a name="p1769224485619"></a>int Warn(const HiLogLabel &label, const char *fmt, ...)</p> 47</td> 48<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1969194465613"><a name="p1969194465613"></a><a name="p1969194465613"></a>HILOG_WARN(type, ...)</p> 49</td> 50</tr> 51<tr id="row45448547369"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p4748102365615"><a name="p4748102365615"></a><a name="p4748102365615"></a>int Error(const HiLogLabel &label, const char *fmt, ...)</p> 52</td> 53<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p15747142375613"><a name="p15747142375613"></a><a name="p15747142375613"></a>HILOG_ERROR(type, ...)</p> 54</td> 55</tr> 56<tr id="row12545125453610"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p5745132318560"><a name="p5745132318560"></a><a name="p5745132318560"></a>int Fatal(const HiLogLabel &label, const char *fmt, ...)</p> 57</td> 58<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p107441123175615"><a name="p107441123175615"></a><a name="p107441123175615"></a>HILOG_FATAL(type, ...)</p> 59</td> 60</tr> 61<tr id="row1454565433611"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1574242345618"><a name="p1574242345618"></a><a name="p1574242345618"></a>NA</p> 62</td> 63<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p197416239562"><a name="p197416239562"></a><a name="p197416239562"></a>int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...)</p> 64</td> 65</tr> 66<tr id="row165451854193617"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p64411913343"><a name="p64411913343"></a><a name="p64411913343"></a>boolean IsLoggable(unsigned int domain, const char *tag, LogLevel level)</p> 67</td> 68<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p107388238561"><a name="p107388238561"></a><a name="p107388238561"></a>bool HiLogIsLoggable(unsigned int domain, const char *tag, LogLevel level)</p> 69</td> 70</tr> 71<tr id="row154535415367"><td class="cellrowborder" valign="top" width="18.258174182581744%" headers="mcps1.2.4.1.1 "><p id="p795821952317"><a name="p795821952317"></a><a name="p795821952317"></a>HiLogLabel</p> 72</td> 73<td class="cellrowborder" valign="top" width="43.39566043395661%" headers="mcps1.2.4.1.2 "><p id="p20464133712224"><a name="p20464133712224"></a><a name="p20464133712224"></a>struct HiLogLabel</p> 74</td> 75<td class="cellrowborder" valign="top" width="38.34616538346165%" headers="mcps1.2.4.1.3 "><p id="p1360616314276"><a name="p1360616314276"></a><a name="p1360616314276"></a>LOG_DOMAIN</p> 76<p id="p4698145992513"><a name="p4698145992513"></a><a name="p4698145992513"></a>LOG_TAG</p> 77</td> 78</tr> 79</tbody> 80</table> 81 82**Table 2** Parameters of C++ APIs 83 84<a name="table19597131833715"></a> 85<table><thead align="left"><tr id="row7839141817375"><th class="cellrowborder" valign="top" width="8.27%" id="mcps1.2.4.1.1"><p id="p383919182379"><a name="p383919182379"></a><a name="p383919182379"></a><strong id="b1768458647"><a name="b1768458647"></a><a name="b1768458647"></a>Class</strong></p> 86</th> 87<th class="cellrowborder" valign="top" width="31.41%" id="mcps1.2.4.1.2"><p id="p168392018203711"><a name="p168392018203711"></a><a name="p168392018203711"></a><strong id="b278081916236"><a name="b278081916236"></a><a name="b278081916236"></a>API</strong></p> 88</th> 89<th class="cellrowborder" valign="top" width="60.31999999999999%" id="mcps1.2.4.1.3"><p id="p168391618193717"><a name="p168391618193717"></a><a name="p168391618193717"></a><strong id="b399622116235"><a name="b399622116235"></a><a name="b399622116235"></a>Description</strong></p> 90</th> 91</tr> 92</thead> 93<tbody><tr id="row383911183378"><td class="cellrowborder" rowspan="6" valign="top" width="8.27%" headers="mcps1.2.4.1.1 "><p id="p10839318133713"><a name="p10839318133713"></a><a name="p10839318133713"></a>HiLog</p> 94<p id="p58561944151216"><a name="p58561944151216"></a><a name="p58561944151216"></a></p> 95<p id="p5856744201215"><a name="p5856744201215"></a><a name="p5856744201215"></a></p> 96<p id="p1985664413120"><a name="p1985664413120"></a><a name="p1985664413120"></a></p> 97<p id="p138562442123"><a name="p138562442123"></a><a name="p138562442123"></a></p> 98<p id="p485612447125"><a name="p485612447125"></a><a name="p485612447125"></a></p> 99</td> 100<td class="cellrowborder" valign="top" width="31.41%" headers="mcps1.2.4.1.2 "><p id="p12483951497"><a name="p12483951497"></a><a name="p12483951497"></a>int Debug(const HiLogLabel &label, const char *fmt, ...)</p> 101</td> 102<td class="cellrowborder" valign="top" width="60.31999999999999%" headers="mcps1.2.4.1.3 "><p id="p684013182375"><a name="p684013182375"></a><a name="p684013182375"></a>Generates debug logs.</p> 103<p id="p1384081812377"><a name="p1384081812377"></a><a name="p1384081812377"></a>Input arguments:</p> 104<a name="ul169441595136"></a><a name="ul169441595136"></a><ul id="ul169441595136"><li><strong id="b19571183520407"><a name="b19571183520407"></a><a name="b19571183520407"></a>label</strong>: Identifies the log type, service domain, and tag.</li><li><strong id="b1283512366405"><a name="b1283512366405"></a><a name="b1283512366405"></a>format</strong>: Indicates the constant format, including the parameter type and privacy identifier. If the privacy identifier is not specified, a parameter is treated as a privacy parameter by default.</li><li><strong id="b095016383402"><a name="b095016383402"></a><a name="b095016383402"></a>fmt</strong>: Indicates the string describing the format variable parameter.</li></ul> 105<p id="p591585615123"><a name="p591585615123"></a><a name="p591585615123"></a>Output arguments: none</p> 106<p id="p1620144161310"><a name="p1620144161310"></a><a name="p1620144161310"></a>Return value: Returns a value greater than or equal to <strong id="b267625003111"><a name="b267625003111"></a><a name="b267625003111"></a>0</strong> if the operation is successful; returns a value less than <strong id="b525912610327"><a name="b525912610327"></a><a name="b525912610327"></a>0</strong> otherwise.</p> 107</td> 108</tr> 109<tr id="row16840101803720"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p482832616141"><a name="p482832616141"></a><a name="p482832616141"></a>int Info(const HiLogLabel &label, const char *fmt, ...)</p> 110</td> 111<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p9562039395"><a name="p9562039395"></a><a name="p9562039395"></a>Generates info logs.</p> 112<p id="p198849129150"><a name="p198849129150"></a><a name="p198849129150"></a>Arguments: See argument description of the <strong id="b672105414170"><a name="b672105414170"></a><a name="b672105414170"></a>Debug</strong> function.</p> 113</td> 114</tr> 115<tr id="row198401818193712"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1705727151413"><a name="p1705727151413"></a><a name="p1705727151413"></a>int Warn(const HiLogLabel &label, const char *fmt, ...)</p> 116</td> 117<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p873814715151"><a name="p873814715151"></a><a name="p873814715151"></a>Generates warn logs.</p> 118<p id="p17738847151515"><a name="p17738847151515"></a><a name="p17738847151515"></a>Arguments: See argument description of the <strong id="b36421511178"><a name="b36421511178"></a><a name="b36421511178"></a>Debug</strong> function.</p> 119</td> 120</tr> 121<tr id="row118401118203714"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p55044284141"><a name="p55044284141"></a><a name="p55044284141"></a>int Error(const HiLogLabel &label, const char *fmt, ...)</p> 122</td> 123<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1558516489153"><a name="p1558516489153"></a><a name="p1558516489153"></a>Generates error logs.</p> 124<p id="p1558534841512"><a name="p1558534841512"></a><a name="p1558534841512"></a>Arguments: See argument description of the <strong id="b752514831712"><a name="b752514831712"></a><a name="b752514831712"></a>Debug</strong> function.</p> 125</td> 126</tr> 127<tr id="row3840171813374"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p8306152914144"><a name="p8306152914144"></a><a name="p8306152914144"></a>int Fatal(const HiLogLabel &label, const char *fmt, ...)</p> 128</td> 129<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p625734912152"><a name="p625734912152"></a><a name="p625734912152"></a>Generates fatal logs.</p> 130<p id="p1125712491157"><a name="p1125712491157"></a><a name="p1125712491157"></a>Arguments: See argument description of the <strong id="b16446443111715"><a name="b16446443111715"></a><a name="b16446443111715"></a>Debug</strong> function.</p> 131</td> 132</tr> 133<tr id="row6840818193716"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p421952291617"><a name="p421952291617"></a><a name="p421952291617"></a>boolean IsLoggable(unsigned int domain, const char *tag, LogLevel level)</p> 134</td> 135<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p3722152951615"><a name="p3722152951615"></a><a name="p3722152951615"></a>Checks whether logs of the specified service domain, tag, and level can be printed.</p> 136<p id="p117221929201613"><a name="p117221929201613"></a><a name="p117221929201613"></a>Input arguments:</p> 137<a name="ul1372214296164"></a><a name="ul1372214296164"></a><ul id="ul1372214296164"><li><strong id="b0822517410"><a name="b0822517410"></a><a name="b0822517410"></a>domain</strong>: Indicates the service domain of logs.</li><li><strong id="b953419064117"><a name="b953419064117"></a><a name="b953419064117"></a>tag</strong>: Indicates the log tag.</li><li><strong id="b6256659194017"><a name="b6256659194017"></a><a name="b6256659194017"></a>level</strong>: Indicates the log level.</li></ul> 138<p id="p572242911167"><a name="p572242911167"></a><a name="p572242911167"></a>Output arguments: none</p> 139<p id="p6722162991617"><a name="p6722162991617"></a><a name="p6722162991617"></a>Return value: Returns <strong id="b8852118175419"><a name="b8852118175419"></a><a name="b8852118175419"></a>true</strong> if the specified logs can be printed; returns <strong id="b1019141735419"><a name="b1019141735419"></a><a name="b1019141735419"></a>false</strong> otherwise.</p> 140</td> 141</tr> 142<tr id="row15841191813371"><td class="cellrowborder" valign="top" width="8.27%" headers="mcps1.2.4.1.1 "><p id="p98771141182714"><a name="p98771141182714"></a><a name="p98771141182714"></a>HiLogLabel</p> 143</td> 144<td class="cellrowborder" valign="top" width="31.41%" headers="mcps1.2.4.1.2 "><p id="p137234972715"><a name="p137234972715"></a><a name="p137234972715"></a>struct HiLogLabel</p> 145</td> 146<td class="cellrowborder" valign="top" width="60.31999999999999%" headers="mcps1.2.4.1.3 "><p id="p48419181372"><a name="p48419181372"></a><a name="p48419181372"></a>Initializes log tag parameters.</p> 147<p id="p335055115288"><a name="p335055115288"></a><a name="p335055115288"></a>Members:</p> 148<a name="ul1235085115287"></a><a name="ul1235085115287"></a><ul id="ul1235085115287"><li><strong id="b521813144117"><a name="b521813144117"></a><a name="b521813144117"></a>type</strong>: Indicates the log type.</li><li><strong id="b1014691044111"><a name="b1014691044111"></a><a name="b1014691044111"></a>domain</strong>: Indicates the service domain of logs.</li><li><strong id="b1843710115416"><a name="b1843710115416"></a><a name="b1843710115416"></a>tag</strong>: Indicates the log tag.</li></ul> 149</td> 150</tr> 151</tbody> 152</table> 153 154## How to Develop<a name="section102728581536"></a> 155 156### C <a name="section12916224185417"></a> 157 1581. Include the **hilog** header file in the **.c** source file. 159 160 ``` 161 #include "hilog/log.h" 162 ``` 163 164 Construct **domain** and **tag**. 165 166 ``` 167 #undef LOG_DOMAIN 168 #undef LOG_TAG 169 #define LOG_DOMAIN 0xD003200 // Indicates the service domain. The value ranges from 0xD000000 to 0xFFFFF. 170 #define LOG_TAG "MY_TAG" 171 ``` 172 173 Print logs. 174 175 ``` 176 HILOG_INFO(LOG_CORE, "Failed to visit %{private}s, reason:%{public}d.", url, errno); 177 ``` 178 1792. Configure compilation information. Specifically, add the subsystem SDK dependency to **BUILD.gn**. 180 181 ``` 182 external_deps = [ "hilog_native:libhilog" ] 183 ``` 184 185 186### C++ <a name="section19399185610547"></a> 187 1881. Include the **hilog** header file in the **.h** class definition header file. 189 190 ``` 191 #include "hilog/log.h" 192 ``` 193 194 If log printing is required for the class header file, define **LABEL** at the beginning of the class definition in the header file. 195 196 ``` 197 class MyClass { 198 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD003200, "MY_TAG"}; 199 ... 200 } 201 ``` 202 203 If log printing is not required for the class definition header file, define **LABEL** in the class implementation file. 204 205 ``` 206 using namespace OHOS::HiviewDFX; 207 static constexpr HiLogLabel LABEL = {LOG_CORE, 0xD003200, "MY_TAG"}; 208 ``` 209 210 Print logs. 211 212 ``` 213 HiLog::Info(LABEL, "Failed to visit %{private}s, reason:%{public}d.", url, errno); 214 ``` 215 2162. Configure compilation information. Specifically, add the subsystem SDK dependency to **BUILD.gn**. 217 218 ``` 219 external_deps = [ "hiviewdfx:libhilog" ] 220 ``` 221 222 223