Skip to content

Commit

Permalink
Change tfm::format for no stream version to tfm::strformat
Browse files Browse the repository at this point in the history
The linter is confused by the overload of tfm::format to return
a string instead of the first argument being a stream. Using a
different function name solves the problem.
  • Loading branch information
jamescowens committed Mar 27, 2022
1 parent 71e99a7 commit 2d87b6a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
22 changes: 11 additions & 11 deletions src/gridcoin/scraper/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void Http::Download(
ScopedFile fp(fsbridge::fopen(destination, "wb"), &fclose);
if (!fp)
throw std::runtime_error(
tfm::format("Error opening target %s: %s (%d)", destination, strerror(errno), errno));
tfm::strformat("Error opening target %s: %s (%d)", destination, strerror(errno), errno));

std::string buffer;
ScopedCurl curl = GetContext();
Expand All @@ -221,7 +221,7 @@ void Http::Download(

CURLcode res = curl_easy_perform(curl.get());
if (res > 0)
throw std::runtime_error(tfm::format("Failed to download file %s: %s", url, curl_easy_strerror(res)));
throw std::runtime_error(tfm::strformat("Failed to download file %s: %s", url, curl_easy_strerror(res)));
}

std::string Http::GetEtag(
Expand All @@ -247,7 +247,7 @@ std::string Http::GetEtag(
curl_slist_free_all(headers);

if (res > 0)
throw std::runtime_error(tfm::format("Failed to get ETag for URL %s: %s", url, curl_easy_strerror(res)));
throw std::runtime_error(tfm::strformat("Failed to get ETag for URL %s: %s", url, curl_easy_strerror(res)));

// Validate HTTP return code.
long response_code;
Expand Down Expand Up @@ -291,7 +291,7 @@ std::string Http::GetLatestVersionResponse()
CURLcode res = curl_easy_perform(curl.get());

if (res > 0)
throw std::runtime_error(tfm::format("Failed to get version response from URL %s: %s",
throw std::runtime_error(tfm::strformat("Failed to get version response from URL %s: %s",
url, curl_easy_strerror(res)));

curl_slist_free_all(headers);
Expand Down Expand Up @@ -320,7 +320,7 @@ void Http::DownloadSnapshot()
DownloadStatus.SetSnapshotDownloadFailed(true);

throw std::runtime_error(
tfm::format("Snapshot Downloader: Error opening target %s: %s (%d)",
tfm::strformat("Snapshot Downloader: Error opening target %s: %s (%d)",
destination.string(), strerror(errno), errno));
}

Expand Down Expand Up @@ -374,7 +374,7 @@ void Http::DownloadSnapshot()
{
DownloadStatus.SetSnapshotDownloadFailed(true);

throw std::runtime_error(tfm::format("Snapshot Downloader: Failed to download file %s: %s",
throw std::runtime_error(tfm::strformat("Snapshot Downloader: Failed to download file %s: %s",
url, curl_easy_strerror(res)));
}
}
Expand Down Expand Up @@ -458,13 +458,13 @@ void Http::EvaluateResponse(int code, const std::string& url)
code == 308)
return;
else if (code == 400)
throw HttpException(tfm::format("Server returned a http code of Bad Request <url=%s, code=%d>", url, code));
throw HttpException(tfm::strformat("Server returned a http code of Bad Request <url=%s, code=%d>", url, code));
else if (code == 401)
throw HttpException(tfm::format("Server returned a http code of Unauthorized <url=%s, code=%d>", url, code));
throw HttpException(tfm::strformat("Server returned a http code of Unauthorized <url=%s, code=%d>", url, code));
else if (code == 403)
throw HttpException(tfm::format("Server returned a http code of Forbidden <url=%s, code=%d>", url, code));
throw HttpException(tfm::strformat("Server returned a http code of Forbidden <url=%s, code=%d>", url, code));
else if (code == 404)
throw HttpException(tfm::format("Server returned a http code of Not Found <url=%s, code=%d>", url, code));
throw HttpException(tfm::strformat("Server returned a http code of Not Found <url=%s, code=%d>", url, code));

throw HttpException(tfm::format("Server returned a http code <url=%s, code=%d>", url, code));
throw HttpException(tfm::strformat("Server returned a http code <url=%s, code=%d>", url, code));
}
6 changes: 3 additions & 3 deletions src/gridcoin/scraper/scraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ void _log(logattribute eType, const std::string& sCall, const std::string& sMess
return;
}

sOut = tfm::format("%s [%s] <%s> : %s", DateTimeStrFormat("%x %H:%M:%S", GetAdjustedTime()), sType, sCall, sMessage);
sOut = tfm::strformat("%s [%s] <%s> : %s", DateTimeStrFormat("%x %H:%M:%S", GetAdjustedTime()), sType, sCall, sMessage);

// This snoops the SCRAPER category setting from the main logger, and uses it as a conditional for the scraper log output.
// Logs will be posted to the scraper log when the category is set OR it is not an INFO level... (i.e. critical, warning,
Expand Down Expand Up @@ -1320,7 +1320,7 @@ void ApplyCache(const std::string& key, T& result)
}
catch (const std::exception&)
{
_log(logattribute::ERR, "ScraperApplyAppCacheEntries", tfm::format("Ignoring bad AppCache entry for %s.", key));
_log(logattribute::ERR, "ScraperApplyAppCacheEntries", tfm::strformat("Ignoring bad AppCache entry for %s.", key));
}
}

Expand Down Expand Up @@ -2384,7 +2384,7 @@ EXCLUSIVE_LOCKS_REQUIRED(cs_TeamIDMap)

if (!ParseInt64(sTeamID, &nTeamID))
{
_log(logattribute::ERR, __func__, tfm::format("Ignoring bad team id for team %s.", sTeamName));
_log(logattribute::ERR, __func__, tfm::strformat("Ignoring bad team id for team %s.", sTeamName));
continue;
}

Expand Down
8 changes: 8 additions & 0 deletions src/tinyformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,14 @@ std::string format(const std::string &fmt, const Args&... args)
return oss.str();
}

// This is to make the linter happy because it the variadic script/python variadic checker does not deal with
// overloads.
template<typename... Args>
std::string strformat(const std::string &fmt, const Args&... args)
{
return format(fmt, args...);
}

} // namespace tinyformat

/** Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for details) */
Expand Down
1 change: 1 addition & 0 deletions test/lint/lint-format-strings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ FUNCTION_NAMES_AND_NUMBER_OF_LEADING_ARGUMENTS=(
"FatalError,0"
"fprintf,1"
"tfm::format,1" # Assuming tfm::::format(std::ostream&, ...
"tfm::strformat,0" # Assuming tfm::::strformat(fmt, ...
"LogConnectFailure,1"
"LogPrint,1"
"LogPrintf,0"
Expand Down

0 comments on commit 2d87b6a

Please sign in to comment.