| 
49 | 49 | #include "pb_preferred_memory.h"  | 
50 | 50 | #include "pb_response_iterator.h"  | 
51 | 51 | #include "pb_string.h"  | 
 | 52 | +#include "pb_stub_log.h"  | 
52 | 53 | #include "pb_utils.h"  | 
53 | 54 | #include "response_sender.h"  | 
54 | 55 | #include "scoped_defer.h"  | 
@@ -1569,138 +1570,6 @@ Stub::ProcessBLSResponseDecoupled(std::unique_ptr<IPCMessage>& ipc_message)  | 
1569 | 1570 |   }  | 
1570 | 1571 | }  | 
1571 | 1572 | 
 
  | 
1572 |  | -std::unique_ptr<Logger> Logger::log_instance_;  | 
1573 |  | - | 
1574 |  | -std::unique_ptr<Logger>&  | 
1575 |  | -Logger::GetOrCreateInstance()  | 
1576 |  | -{  | 
1577 |  | -  if (Logger::log_instance_.get() == nullptr) {  | 
1578 |  | -    Logger::log_instance_ = std::make_unique<Logger>();  | 
1579 |  | -  }  | 
1580 |  | - | 
1581 |  | -  return Logger::log_instance_;  | 
1582 |  | -}  | 
1583 |  | - | 
1584 |  | -// Bound function, called from the python client  | 
1585 |  | -void  | 
1586 |  | -Logger::Log(const std::string& message, LogLevel level)  | 
1587 |  | -{  | 
1588 |  | -  std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance();  | 
1589 |  | -  py::object frame = py::module_::import("inspect").attr("currentframe");  | 
1590 |  | -  py::object caller_frame = frame();  | 
1591 |  | -  py::object info = py::module_::import("inspect").attr("getframeinfo");  | 
1592 |  | -  py::object caller_info = info(caller_frame);  | 
1593 |  | -  py::object filename_python = caller_info.attr("filename");  | 
1594 |  | -  std::string filename = filename_python.cast<std::string>();  | 
1595 |  | -  py::object lineno = caller_info.attr("lineno");  | 
1596 |  | -  uint32_t line = lineno.cast<uint32_t>();  | 
1597 |  | - | 
1598 |  | -  if (!stub->StubToParentServiceActive()) {  | 
1599 |  | -    Logger::GetOrCreateInstance()->Log(filename, line, level, message);  | 
1600 |  | -  } else {  | 
1601 |  | -    std::unique_ptr<PbLog> log_msg(new PbLog(filename, line, message, level));  | 
1602 |  | -    stub->EnqueueLogRequest(log_msg);  | 
1603 |  | -  }  | 
1604 |  | -}  | 
1605 |  | - | 
1606 |  | -// Called internally (.e.g. LOG_ERROR << "Error"; )  | 
1607 |  | -void  | 
1608 |  | -Logger::Log(  | 
1609 |  | -    const std::string& filename, uint32_t lineno, LogLevel level,  | 
1610 |  | -    const std::string& message)  | 
1611 |  | -{  | 
1612 |  | -  // If the log monitor service is not active yet, format  | 
1613 |  | -  // and pass messages to cerr  | 
1614 |  | -  if (!BackendLoggingActive()) {  | 
1615 |  | -    std::string path(filename);  | 
1616 |  | -    size_t pos = path.rfind(std::filesystem::path::preferred_separator);  | 
1617 |  | -    if (pos != std::string::npos) {  | 
1618 |  | -      path = path.substr(pos + 1, std::string::npos);  | 
1619 |  | -    }  | 
1620 |  | -#ifdef _WIN32  | 
1621 |  | -    std::stringstream ss;  | 
1622 |  | -    SYSTEMTIME system_time;  | 
1623 |  | -    GetSystemTime(&system_time);  | 
1624 |  | -    ss << LeadingLogChar(level) << std::setfill('0') << std::setw(2)  | 
1625 |  | -       << system_time.wMonth << std::setw(2) << system_time.wDay << ' '  | 
1626 |  | -       << std::setw(2) << system_time.wHour << ':' << std::setw(2)  | 
1627 |  | -       << system_time.wMinute << ':' << std::setw(2) << system_time.wSecond  | 
1628 |  | -       << '.' << std::setw(6) << system_time.wMilliseconds * 1000 << ' '  | 
1629 |  | -       << static_cast<uint32_t>(GetCurrentProcessId()) << ' ' << path << ':'  | 
1630 |  | -       << lineno << "] ";  | 
1631 |  | -#else  | 
1632 |  | -    std::stringstream ss;  | 
1633 |  | -    struct timeval tv;  | 
1634 |  | -    gettimeofday(&tv, NULL);  | 
1635 |  | -    struct tm tm_time;  | 
1636 |  | -    gmtime_r(((time_t*)&(tv.tv_sec)), &tm_time);  | 
1637 |  | -    ss << LeadingLogChar(level) << std::setfill('0') << std::setw(2)  | 
1638 |  | -       << (tm_time.tm_mon + 1) << std::setw(2) << tm_time.tm_mday << " "  | 
1639 |  | -       << std::setw(2) << tm_time.tm_hour << ':' << std::setw(2)  | 
1640 |  | -       << tm_time.tm_min << ':' << std::setw(2) << tm_time.tm_sec << "."  | 
1641 |  | -       << std::setw(6) << tv.tv_usec << ' ' << static_cast<uint32_t>(getpid())  | 
1642 |  | -       << ' ' << path << ':' << lineno << "] ";  | 
1643 |  | -    std::cerr << ss.str() << " " << message << std::endl;  | 
1644 |  | -#endif  | 
1645 |  | -  } else {  | 
1646 |  | -    // Ensure we do not create a stub instance before it has initialized  | 
1647 |  | -    std::unique_ptr<Stub>& stub = Stub::GetOrCreateInstance();  | 
1648 |  | -    std::unique_ptr<PbLog> log_msg(new PbLog(filename, lineno, message, level));  | 
1649 |  | -    stub->EnqueueLogRequest(log_msg);  | 
1650 |  | -  }  | 
1651 |  | -}  | 
1652 |  | - | 
1653 |  | -void  | 
1654 |  | -Logger::LogInfo(const std::string& message)  | 
1655 |  | -{  | 
1656 |  | -  Logger::Log(message, LogLevel::kInfo);  | 
1657 |  | -}  | 
1658 |  | - | 
1659 |  | -void  | 
1660 |  | -Logger::LogWarn(const std::string& message)  | 
1661 |  | -{  | 
1662 |  | -  Logger::Log(message, LogLevel::kWarning);  | 
1663 |  | -}  | 
1664 |  | - | 
1665 |  | -void  | 
1666 |  | -Logger::LogError(const std::string& message)  | 
1667 |  | -{  | 
1668 |  | -  Logger::Log(message, LogLevel::kError);  | 
1669 |  | -}  | 
1670 |  | - | 
1671 |  | -void  | 
1672 |  | -Logger::LogVerbose(const std::string& message)  | 
1673 |  | -{  | 
1674 |  | -  Logger::Log(message, LogLevel::kVerbose);  | 
1675 |  | -}  | 
1676 |  | - | 
1677 |  | -const std::string  | 
1678 |  | -Logger::LeadingLogChar(const LogLevel& level)  | 
1679 |  | -{  | 
1680 |  | -  switch (level) {  | 
1681 |  | -    case LogLevel::kWarning:  | 
1682 |  | -      return "W";  | 
1683 |  | -    case LogLevel::kError:  | 
1684 |  | -      return "E";  | 
1685 |  | -    case LogLevel::kInfo:  | 
1686 |  | -    case LogLevel::kVerbose:  | 
1687 |  | -    default:  | 
1688 |  | -      return "I";  | 
1689 |  | -  }  | 
1690 |  | -}  | 
1691 |  | - | 
1692 |  | -void  | 
1693 |  | -Logger::SetBackendLoggingActive(bool status)  | 
1694 |  | -{  | 
1695 |  | -  backend_logging_active_ = status;  | 
1696 |  | -}  | 
1697 |  | - | 
1698 |  | -bool  | 
1699 |  | -Logger::BackendLoggingActive()  | 
1700 |  | -{  | 
1701 |  | -  return backend_logging_active_;  | 
1702 |  | -}  | 
1703 |  | - | 
1704 | 1573 | PYBIND11_EMBEDDED_MODULE(c_python_backend_utils, module)  | 
1705 | 1574 | {  | 
1706 | 1575 |   py::class_<PbError, std::shared_ptr<PbError>> triton_error(  | 
 | 
0 commit comments