blob: 45aa3de2a3c6ff5a01793a242c3e1d9676a77492 [file] [log] [blame]
Marri Devender Rao78479602020-01-06 03:45:11 -06001#include "phal_error.hpp"
2
3#include "create_pel.hpp"
4
5#include <libekb.H>
6#include <libipl.H>
7
8#include <iomanip>
9#include <phosphor-logging/elog.hpp>
10#include <sstream>
11
12namespace openpower
13{
14namespace pel
15{
16using namespace phosphor::logging;
17
18namespace detail
19{
20
21// keys need to be unique so using counter value to generate unique key
22static int counter = 0;
23
24// list of debug traces
25static std::vector<std::pair<std::string, std::string>> traceLog;
26
27void processLogTraceCallback(void* private_data, const char* fmt, va_list ap)
28{
29 va_list vap;
30 va_copy(vap, ap);
31 std::vector<char> log(1 + std::vsnprintf(nullptr, 0, fmt, ap));
32 std::vsnprintf(log.data(), log.size(), fmt, vap);
33 va_end(vap);
34 std::string logstr(log.begin(), log.end());
35
36 // ignore stray characters in log traces coming from ekb
37 // example: "\n", which are good for print to screen
38 if (logstr.length() < 5)
39 {
40 return;
41 }
42
43 char timeBuf[80];
44 time_t t = time(0);
45 tm myTm{};
46 gmtime_r(&t, &myTm);
47 strftime(timeBuf, 80, "%Y-%m-%d %H:%M:%S", &myTm);
48
49 // key values need to be unique for PEL
50 // TODO #openbmc/dev/issues/1563
51 // If written to Json no need to worry about unique KEY
52 std::stringstream str;
53 str << std::setfill('0');
54 str << "LOG" << std::setw(3) << counter;
55 str << " " << timeBuf;
56 traceLog.emplace_back(std::make_pair(str.str(), std::move(logstr)));
57 counter++;
58}
59
60void processBootErrorCallback(bool status)
61{
62 log<level::INFO>("processBootCallback ", entry("STATUS=%d", status));
63 try
64 {
65 // If failure in hwp execution
66 if (!status)
67 {
68 FFDCData ffdc = libekb_get_ffdc();
69 ffdc.insert(ffdc.end(), traceLog.begin(), traceLog.end());
70 openpower::pel::createBootErrorPEL(ffdc);
71 }
72 }
73 catch (std::exception& ex)
74 {
75 reset();
76 throw ex;
77 }
78 reset();
79}
80
81void reset()
82{
83 // reset the trace log and counter
84 traceLog.clear();
85 counter = 0;
86}
87} // namespace detail
88
89void addBootErrorCallbacks()
90{
91 // set log level to info
92 ipl_set_loglevel(IPL_INFO);
93
94 // add callback for debug traces
95 ipl_set_logfunc(detail::processLogTraceCallback, NULL);
96
97 // add callback for ipl failures
Jayanth Othayothaefde692020-03-27 07:56:47 -050098 ipl_set_error_callback_func(detail::processBootErrorCallback);
Marri Devender Rao78479602020-01-06 03:45:11 -060099}
100} // namespace pel
101} // namespace openpower