Cleanup: IO Common: Use clog and fmtlib instead of streams

Use CLOG for warnings and fmtlib for developer output and string
formatting rather than the heavy `<stringstream>` machinery.

Pull Request: https://projects.blender.org/blender/blender/pulls/131126
This commit is contained in:
Jesse Yurkovich
2024-12-03 00:24:01 +01:00
committed by Jesse Yurkovich
parent 000f1cfa64
commit d0eff5f665
7 changed files with 38 additions and 58 deletions

View File

@@ -35,7 +35,9 @@ set(LIB
PRIVATE bf::blenlib
PRIVATE bf::depsgraph
PRIVATE bf::dna
PRIVATE bf::intern::clog
PRIVATE bf::intern::guardedalloc
PRIVATE bf::extern::fmtlib
)
blender_add_lib(bf_io_common "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")

View File

@@ -8,7 +8,6 @@
#include "DNA_object_types.h" /* For MAX_DUPLI_RECUR */
#include <array>
#include <iosfwd>
#include <string>
namespace blender::io {
@@ -41,7 +40,6 @@ class PersistentID {
friend bool operator==(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b);
friend bool operator<(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b);
friend std::ostream &operator<<(std::ostream &os, const PersistentID &persistent_id);
private:
void copy_values_from(const PIDArray &persistent_id_values);

View File

@@ -4,12 +4,10 @@
#include "IO_abstract_hierarchy_iterator.h"
#include "dupli_parent_finder.hh"
#include <climits>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <fmt/core.h>
#include "BKE_anim_data.hh"
#include "BKE_duplilist.hh"
#include "BKE_key.hh"
@@ -230,36 +228,37 @@ void AbstractHierarchyIterator::debug_print_export_graph(const ExportGraph &grap
const Object *const duplicator = parent_info.duplicated_by;
if (duplicator != nullptr) {
printf(" DU %s (as dupped by %s):\n",
export_parent == nullptr ? "-null-" : (export_parent->id.name + 2),
duplicator->id.name + 2);
fmt::println(" DU {} (as dupped by {}):",
export_parent == nullptr ? "-null-" : (export_parent->id.name + 2),
duplicator->id.name + 2);
}
else {
printf(" OB %s:\n", export_parent == nullptr ? "-null-" : (export_parent->id.name + 2));
fmt::println(" OB {}:",
export_parent == nullptr ? "-null-" : (export_parent->id.name + 2));
}
total_graph_size += map_iter.second.size();
for (HierarchyContext *child_ctx : map_iter.second) {
if (child_ctx->duplicator == nullptr) {
printf(" - %s%s%s\n",
child_ctx->export_name.c_str(),
child_ctx->weak_export ? " (weak)" : "",
child_ctx->original_export_path.empty() ?
"" :
(std::string("ref ") + child_ctx->original_export_path).c_str());
fmt::println(" - {}{}{}",
child_ctx->export_name.c_str(),
child_ctx->weak_export ? " (weak)" : "",
child_ctx->original_export_path.empty() ?
"" :
(std::string("ref ") + child_ctx->original_export_path).c_str());
}
else {
printf(" - %s (dup by %s%s) %s\n",
child_ctx->export_name.c_str(),
child_ctx->duplicator->id.name + 2,
child_ctx->weak_export ? ", weak" : "",
child_ctx->original_export_path.empty() ?
"" :
(std::string("ref ") + child_ctx->original_export_path).c_str());
fmt::println(" - {} (dup by {}{}) {}",
child_ctx->export_name.c_str(),
child_ctx->duplicator->id.name + 2,
child_ctx->weak_export ? ", weak" : "",
child_ctx->original_export_path.empty() ?
"" :
(std::string("ref ") + child_ctx->original_export_path).c_str());
}
}
}
printf(" (Total graph size: %zu objects)\n", total_graph_size);
fmt::println(" (Total graph size: {} objects)", total_graph_size);
}
void AbstractHierarchyIterator::export_graph_construct()
@@ -458,10 +457,9 @@ void AbstractHierarchyIterator::visit_dupli_object(DupliObject *dupli_object,
copy_m4_m4(context->matrix_world, dupli_object->mat);
/* Construct export name for the dupli-instance. */
std::stringstream export_name_stream;
export_name_stream << get_object_name(context->object) << "-"
<< context->persistent_id.as_object_name_suffix();
context->export_name = make_valid_name(export_name_stream.str());
std::string export_name = get_object_name(context->object) + "-" +
context->persistent_id.as_object_name_suffix();
context->export_name = make_valid_name(export_name);
ExportGraph::key_type graph_index = determine_graph_index_dupli(
context, dupli_object, dupli_parent_finder);

View File

@@ -6,8 +6,6 @@
#include "BLI_utildefines.h"
#include <iostream>
namespace blender::io {
void DupliParentFinder::insert(const DupliObject *dupli_ob)

View File

@@ -5,9 +5,8 @@
#include "dupli_parent_finder.hh"
#include <climits>
#include <cstring>
#include <ostream>
#include <sstream>
#include <fmt/format.h>
namespace blender::io {
@@ -72,7 +71,7 @@ PersistentID PersistentID::instancer_pid() const
std::string PersistentID::as_object_name_suffix() const
{
std::stringstream stream;
fmt::basic_memory_buffer<char, 64> buf;
/* Find one past the last index. */
int index;
@@ -83,13 +82,13 @@ std::string PersistentID::as_object_name_suffix() const
/* Iterate backward to construct the string. */
--index;
for (; index >= 0; --index) {
stream << persistent_id_[index];
fmt::format_to(fmt::appender(buf), "{}", persistent_id_[index]);
if (index > 0) {
stream << "-";
fmt::format_to(fmt::appender(buf), "-");
}
}
return stream.str();
return fmt::to_string(buf);
}
bool operator<(const PersistentID &persistent_id_a, const PersistentID &persistent_id_b)
@@ -133,20 +132,4 @@ bool operator==(const PersistentID &persistent_id_a, const PersistentID &persist
return true;
}
std::ostream &operator<<(std::ostream &os, const PersistentID &persistent_id)
{
if (persistent_id.persistent_id_[0] == INT_MAX) {
return os;
}
const PersistentID::PIDArray &pid_array = persistent_id.persistent_id_;
for (int index = 0; index < PersistentID::array_length_ && pid_array[index] < INT_MAX; ++index) {
if (index > 0) {
os << "-";
}
os << pid_array[index];
}
return os;
}
} // namespace blender::io

View File

@@ -2,8 +2,6 @@
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "BKE_main.hh"
#include "DNA_scene_types.h"
#include "RNA_access.hh"
#include "RNA_types.hh"

View File

@@ -7,6 +7,9 @@
#include "BLI_path_utils.hh"
#include "BLI_string.h"
#include "CLG_log.h"
static CLG_LogRef LOG = {"io.common"};
namespace blender::io {
std::string path_reference(StringRefNull filepath,
@@ -64,18 +67,18 @@ void path_reference_copy(const Set<std::pair<std::string, std::string>> &copy_se
const char *src = copy.first.c_str();
const char *dst = copy.second.c_str();
if (!BLI_exists(src)) {
fprintf(stderr, "Missing source file '%s', not copying\n", src);
CLOG_WARN(&LOG, "Missing source file '%s', not copying", src);
continue;
}
if (0 == BLI_path_cmp_normalized(src, dst)) {
continue; /* Source and destination are the same. */
}
if (!BLI_file_ensure_parent_dir_exists(dst)) {
fprintf(stderr, "Can't make directory for '%s', not copying\n", dst);
CLOG_WARN(&LOG, "Can't make directory for '%s', not copying", dst);
continue;
}
if (BLI_copy(src, dst) != 0) {
fprintf(stderr, "Can't copy '%s' to '%s'\n", src, dst);
CLOG_WARN(&LOG, "Can't copy '%s' to '%s'", src, dst);
continue;
}
}