internal_log.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifdef DBG_LOG
  32. #include "oal/oal.h"
  33. #include "se_wrapper.h"
  34. #include "se_stdio.h"
  35. #include <time.h>
  36. #include <stdio.h>
  37. #include <stdarg.h>
  38. #include <assert.h>
  39. #include "se_thread.h"
  40. static int aesm_trace_level = TRACE_LOG_LEVEL;
  41. static int at_start=1;
  42. ae_error_t load_log_config(void);
  43. se_mutex_t cs;
  44. static ae_error_t init_log_file(void)
  45. {
  46. char filename[MAX_PATH];
  47. ae_error_t err = aesm_get_pathname(FT_PERSISTENT_STORAGE, AESM_DBG_LOG_FID, filename, MAX_PATH);
  48. if(err != AE_SUCCESS)
  49. return err;
  50. (void)load_log_config();
  51. return AE_SUCCESS;
  52. }
  53. #define TIME_BUF_SIZE 100
  54. void aesm_internal_log(const char *file_name, int line_no, const char *funname, int level, const char *format, ...)
  55. {
  56. if(level <= aesm_trace_level){
  57. if(at_start){
  58. at_start=0;
  59. se_mutex_init(&cs);
  60. init_log_file();
  61. }
  62. char filename[MAX_PATH];
  63. ae_error_t err = aesm_get_cpathname(FT_PERSISTENT_STORAGE, AESM_DBG_LOG_FID, filename, MAX_PATH);
  64. if(err != AE_SUCCESS)
  65. return;
  66. FILE *logfile = NULL;
  67. se_mutex_lock(&cs);
  68. errno_t err_code = fopen_s(&logfile, filename, "a+");
  69. if(err_code!=0){
  70. se_mutex_unlock(&cs);
  71. return;
  72. }
  73. time_t t;
  74. struct tm time_info;
  75. va_list varg;
  76. char time_buf[TIME_BUF_SIZE];
  77. time(&t);
  78. struct tm *temp_time_info;
  79. temp_time_info = localtime(&t);
  80. memcpy_s(&time_info, sizeof(time_info), temp_time_info, sizeof(*temp_time_info));
  81. if(strftime(time_buf, TIME_BUF_SIZE, "%c", &time_info)!=0){
  82. fprintf(logfile, "[%s|%d|%s|%s]",file_name, line_no, funname, time_buf);
  83. }else{
  84. fprintf(logfile, "[%s|%d|%s]",file_name, line_no, funname);
  85. }
  86. va_start(varg, format);
  87. vfprintf(logfile, format, varg);
  88. va_end(varg);
  89. fprintf(logfile, "\n");
  90. fflush(logfile);
  91. fclose(logfile);
  92. se_mutex_unlock(&cs);
  93. }
  94. }
  95. void aesm_set_log_level(int level)
  96. {
  97. aesm_trace_level = level;
  98. }
  99. static char half_byte_to_char(int x)
  100. {
  101. assert(0<=x&&x<=0xF);
  102. if(0<=x&&x<=9)return (char)('0'+x);
  103. else return (char)('A'+x-10);
  104. }
  105. void aesm_dbg_format_hex(const uint8_t *data, uint32_t data_len, char *out_buf, uint32_t buf_size)
  106. {
  107. uint32_t i;
  108. assert(buf_size>0);
  109. if(data_len==0){
  110. out_buf[0]='\0';
  111. return;
  112. }
  113. if(buf_size/3>=data_len){
  114. for(i=0;i<data_len;i++){
  115. int low=data[i]&0xF;
  116. int high=(data[i]>>4)&0xF;
  117. out_buf[i*3]=half_byte_to_char(high);
  118. out_buf[i*3+1]=half_byte_to_char(low);
  119. out_buf[i*3+2]=' ';
  120. }
  121. out_buf[data_len*3-1]='\0';
  122. }else if(buf_size>10){
  123. uint32_t tcount=buf_size/3-1;
  124. uint32_t off;
  125. uint32_t ecount=tcount/2,bcount=tcount-ecount;
  126. for(i=0;i<bcount;i++){
  127. int low=data[i]&0xF;
  128. int high=(data[i]>>4)&0xF;
  129. out_buf[i*3]=half_byte_to_char(high);
  130. out_buf[i*3+1]=half_byte_to_char(low);
  131. out_buf[i*3+2]=' ';
  132. }
  133. out_buf[i*3]=out_buf[i*3+1]=out_buf[i*3+2]='.';
  134. off=i*3+3;
  135. for(i=0;i<ecount;i++){
  136. int low=data[data_len-ecount+i]&0xF;
  137. int high=(data[data_len-ecount+i]>>4)&0xF;
  138. out_buf[off+i*3]=half_byte_to_char(high);
  139. out_buf[off+i*3+1]=half_byte_to_char(low);
  140. out_buf[off+i*3+2]=' ';
  141. }
  142. out_buf[off+i*3-1]='\0';
  143. }else{
  144. for(i=0;/*i<data_len&&*/i<(buf_size-1)/3;i++){//checking for i<data_len is redundant since first if condition in the function has filtered it
  145. int low=data[i]&0xF;
  146. int high=(data[i]>>4)&0xF;
  147. out_buf[i*3]=half_byte_to_char(high);
  148. out_buf[i*3+1]=half_byte_to_char(low);
  149. out_buf[i*3+2]=' ';
  150. }
  151. out_buf[i*3]='\0';
  152. }
  153. }
  154. #include "tinyxml.h"
  155. static const char *xml_get_child_text(TiXmlNode *parent, const char *name)
  156. {
  157. if(parent == NULL) return NULL;
  158. TiXmlNode *sub_node = parent->FirstChild(name);
  159. if(sub_node == NULL) return NULL;
  160. TiXmlElement *elem = sub_node->ToElement();
  161. if(elem == NULL) return NULL;
  162. return elem->GetText();
  163. }
  164. static const char *dbg_level_str[]={
  165. "fatal",
  166. "error",
  167. "warning",
  168. "info",
  169. "debug",
  170. "trace"
  171. };
  172. #define DBG_LEVEL_COUNT (sizeof(dbg_level_str)/sizeof(dbg_level_str[0]))
  173. static int find_dbg_level_str(const char *text_level)
  174. {
  175. uint32_t i;
  176. size_t text_level_len = strlen(text_level);
  177. for(i=0;i<DBG_LEVEL_COUNT;i++){
  178. size_t cur_len = strlen(dbg_level_str[i]);
  179. if(cur_len>text_level_len)cur_len=text_level_len;
  180. if(_strnicmp(text_level, dbg_level_str[i], cur_len)==0){
  181. return (int)i;
  182. }
  183. }
  184. AESM_DBG_ERROR("unkown level %s",text_level);
  185. return -1;
  186. }
  187. ae_error_t load_log_config(void)
  188. {
  189. char path_name[MAX_PATH];
  190. ae_error_t ae_err = AE_SUCCESS;
  191. if((ae_err=aesm_get_cpathname(FT_PERSISTENT_STORAGE, AESM_DBG_LOG_CFG_FID, path_name, MAX_PATH))!=AE_SUCCESS){
  192. AESM_DBG_ERROR("fail to read config path");
  193. return ae_err;
  194. }
  195. TiXmlDocument doc(path_name);
  196. bool load_ok = doc.LoadFile();
  197. if(!load_ok){
  198. AESM_DBG_ERROR("fail to load config file %s", path_name);
  199. return OAL_FILE_ACCESS_ERROR;
  200. }
  201. TiXmlNode *pmetadata_node = doc.FirstChild("DbgLog");
  202. const char *temp_text = xml_get_child_text(pmetadata_node, "level");
  203. if(temp_text!=NULL){
  204. if(isdigit(temp_text[0])){
  205. AESM_SET_DBG_LEVEL(atoi(temp_text));
  206. }else{
  207. int level = find_dbg_level_str(temp_text);
  208. if(level>=0){
  209. AESM_SET_DBG_LEVEL(level);
  210. }
  211. }
  212. }else{
  213. AESM_DBG_ERROR("fail to find level");
  214. }
  215. return AE_SUCCESS;
  216. }
  217. #endif