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

Add timestamp on backup files, C++11 version #64

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ function(cloysterhpc_setup_dependencies)

if(NOT TARGET spdlog::spdlog)
if (cloysterhpc_ENABLE_CONAN)
CPMFindPackage(NAME spdlog)
CPMFindPackage(NAME spdlog
OPTIONS
"SPDLOG_FMT_EXTERNAL ON")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that works when using Conan instead of CPM?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not... but I used conan and it worked.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well whatever. That may be an issue at some point but may be not. If not we gonna open a bug for this.

The root of the problem here is that CPM seems to be the way forward for Package Management, but there's some packages that aren't available yet. So we rely on Conan for it.

A bigger issue will be when we move to std::format... what if this PR breaks with it?

Copy link
Collaborator Author

@arthurmco arthurmco Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CPM seems to download packages from github repositories, so the "non-presence" might not be a problem.

Regarding std::format, it supports formatting chrono data types (source):

For chrono types, the format specification is interpreted as chrono format specification.

else()
CPMAddPackage(
NAME
Expand Down
2 changes: 1 addition & 1 deletion cmake/CommonLibraries.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ set(COMMON_LIBS
resolv
${STDC++FS}
doctest::doctest
cryptopp::cryptopp)
cryptopp::cryptopp)
4 changes: 2 additions & 2 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[requires]
cli11/2.3.2
# libfmt version will be automatically selected by spdlog
#fmt/9.1.0
spdlog/[>=1.11.0]
fmt/10.2.1
spdlog/[>=1.14.0]
boost/1.82.0
magic_enum/0.9.2
gsl-lite/0.40.0
Expand Down
1 change: 1 addition & 0 deletions include/cloysterhpc/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void backupFile(std::string_view filename);
void changeValueInConfigurationFile(
const std::string&, const std::string&, std::string_view);
void addStringToFile(std::string_view filename, std::string_view string);
std::string getCurrentTimestamp();

} /* namespace cloyster */

Expand Down
28 changes: 25 additions & 3 deletions src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

#include <cstdlib> /* getenv() */
#include <iostream>
#include <chrono>

#include <boost/process.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <fmt/format.h>

#include <cloysterhpc/services/log.h>

#include <fmt/format.h>
#include <fmt/chrono.h>
#include <fstream>

namespace cloyster {
Expand Down Expand Up @@ -141,10 +143,30 @@ void removeFile(std::string_view filename)
}
}

/**
* \brief Get the current timestamp as a string.
*
* This function generates the current timestamp in the format
* "YYYYMMDD_HHMMSS".
*
* \return A string with the current timestamp.
*/
std::string getCurrentTimestamp()
{
using clock = std::chrono::system_clock;
using sec = std::chrono::seconds;

std::chrono::time_point<clock> current_time = clock::now();
auto result = fmt::format("{:%FT%TZ}", std::chrono::time_point_cast<sec>(current_time));

return result;
}

/* Backup file */
void backupFile(std::string_view filename)
{
const auto& backupFile = fmt::format("{}/backup{}", installPath, filename);
const auto& backupFile = fmt::format(
"{}/backup{}_{}", installPath, filename, getCurrentTimestamp());

if (cloyster::dryRun) {
LOG_INFO(
Expand Down
Loading