Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting TimeStamp #213

Closed
lijh8 opened this issue Jul 2, 2023 · 1 comment
Closed

Formatting TimeStamp #213

lijh8 opened this issue Jul 2, 2023 · 1 comment

Comments

@lijh8
Copy link

lijh8 commented Jul 2, 2023

    auto format = expr::stream
        << expr::attr<boost::posix_time::ptime>("TimeStamp")
        << " [" << std::left << std::setw(7) << std::setfill(' ') << logging::trivial::severity << "] "
        << expr::smessage;

    sink->set_formatter(format);
// 2023-Jul-02 14:08:54.877281 

The code generates data time with month part in abbr letters.

I added two more lines to get data time in all digit format.
It allocates memory manually but asan does not warn on it.

    auto *facet = new boost::posix_time::time_facet("%Y%m%d %H%M%S");
    std::locale::global(std::locale(std::locale(), facet));
// 2023-07-02 14:08:54

Is there a better way to do it?

Thanks

@Lastique
Copy link
Member

Lastique commented Jul 2, 2023

Depends on what you actually need. Here are a couple alternatives:

  1. Setting the global locale is fine, but in case if you want to only set the locale for a given sink, you can call imbue on the sink frontend.
  2. You can avoid creating the locale altogether by using a dedicated formatter for dates and time. It will work slightly more efficiently than the locale.

@Lastique Lastique closed this as completed Jul 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants