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

More gradual fmt conversion #2649

Merged
merged 2 commits into from Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/include/OpenImageIO/errorhandler.h
Expand Up @@ -144,7 +144,7 @@ class OIIO_API ErrorHandler {
//
// Formatted output with printf notation. Use these if you specifically
// want printf-notation, even after format() changes to python notation
// for OIIO 2.1.
// in some future OIIO release.
//
template<typename... Args>
void infof(const char* format, const Args&... args)
Expand Down Expand Up @@ -188,6 +188,52 @@ class OIIO_API ErrorHandler {
#endif
}

//
// Formatted output with std::format notation. Use these if you
// specifically want std::format-notation, even before format() changes
// to the new notation in some future OIIO release.
//
template<typename... Args>
void infofmt(const char* format, const Args&... args)
{
if (verbosity() >= VERBOSE)
info(Strutil::fmt::format(format, args...));
}

template<typename... Args>
void warningfmt(const char* format, const Args&... args)
{
if (verbosity() >= NORMAL)
warning(Strutil::fmt::format(format, args...));
}

template<typename... Args>
void errorfmt(const char* format, const Args&... args)
{
error(Strutil::fmt::format(format, args...));
}

template<typename... Args>
void severefmt(const char* format, const Args&... args)
{
severe(Strutil::fmt::format(format, args...));
}

template<typename... Args>
void messagefmt(const char* format, const Args&... args)
{
if (verbosity() > QUIET)
message(Strutil::fmt::format(format, args...));
}

template<typename... Args>
void debugfmt(const char* format, const Args&... args)
{
#ifndef NDEBUG
debug(Strutil::fmt::format(format, args...));
#endif
}

/// One built-in handler that can always be counted on to be present
/// and just echoes the error messages to the console (stdout or
/// stderr, depending on the error category).
Expand Down
11 changes: 10 additions & 1 deletion src/include/OpenImageIO/imagebuf.h
Expand Up @@ -940,7 +940,7 @@ class OIIO_API ImageBuf {
/// Error reporting for ImageBuf: call this with Python / {fmt} /
/// std::format style formatting specification.
template<typename... Args>
void fmterror(const char* fmt, const Args&... args) const
void errorfmt(const char* fmt, const Args&... args) const
{
error(Strutil::fmt::format(fmt, args...));
}
Expand All @@ -962,6 +962,15 @@ class OIIO_API ImageBuf {
error(Strutil::format(fmt, args...));
}

// Error reporting for ImageBuf: call this with Python / {fmt} /
// std::format style formatting specification.
template<typename... Args>
OIIO_DEPRECATED("use `errorfmt` instead")
void fmterror(const char* fmt, const Args&... args) const
{
error(Strutil::fmt::format(fmt, args...));
}

/// Returns `true` if the ImageBuf has had an error and has an error
/// message ready to retrieve via `geterror()`.
bool has_error(void) const;
Expand Down
26 changes: 25 additions & 1 deletion src/include/OpenImageIO/imageio.h
Expand Up @@ -1595,6 +1595,14 @@ class OIIO_API ImageInput {
/// Error reporting for the plugin implementation: call this with
/// fmt::format-like arguments.
template<typename... Args>
void errorfmt(const char* fmt, const Args&... args) const {
append_error(Strutil::fmt::format (fmt, args...));
}

// Error reporting for the plugin implementation: call this with
// fmt::format-like arguments.
template<typename... Args>
OIIO_DEPRECATED("use `errorfmt` instead")
void fmterror(const char* fmt, const Args&... args) const {
append_error(Strutil::fmt::format (fmt, args...));
}
Expand Down Expand Up @@ -2208,6 +2216,14 @@ class OIIO_API ImageOutput {
/// Error reporting for the plugin implementation: call this with
/// fmt::format-like arguments.
template<typename... Args>
void errorfmt(const char* fmt, const Args&... args) const {
append_error(Strutil::fmt::format (fmt, args...));
}

// Error reporting for the plugin implementation: call this with
// fmt::format-like arguments.
template<typename... Args>
OIIO_DEPRECATED("use `errorfmt` instead")
void fmterror(const char* fmt, const Args&... args) const {
append_error(Strutil::fmt::format (fmt, args...));
}
Expand Down Expand Up @@ -2722,8 +2738,16 @@ typedef bool (*wrap_impl) (int &coord, int origin, int width);
/// output to stderr for debugging statements.
OIIO_API void debug (string_view str);

/// debug output with `fmt`/`std::format` conventions.
/// debug output with `std::format` conventions.
template<typename T1, typename... Args>
void debugfmt (const char* fmt, const T1& v1, const Args&... args)
{
debug (Strutil::fmt::format(fmt, v1, args...));
}

// (Unfortunate old synonym)
template<typename T1, typename... Args>
OIIO_DEPRECATED("use `debugfmt` instead")
void fmtdebug (const char* fmt, const T1& v1, const Args&... args)
{
debug (Strutil::fmt::format(fmt, v1, args...));
Expand Down
1 change: 1 addition & 0 deletions src/include/OpenImageIO/strutil.h
Expand Up @@ -236,6 +236,7 @@ std::string OIIO_API vsprintf (const char *fmt, va_list ap)
/// Return a std::string formatted like Strutil::format, but passed
/// already as a va_list. This is not guaranteed type-safe and is not
/// extensible like format(). Use with caution!
OIIO_DEPRECATED("use `vsprintf` instead")
std::string OIIO_API vformat (const char *fmt, va_list ap)
OPENIMAGEIO_PRINTF_ARGS(1,0);

Expand Down
17 changes: 16 additions & 1 deletion src/include/OpenImageIO/typedesc.h
Expand Up @@ -489,6 +489,9 @@ struct tostring_formatting {
const char *uint_fmt = "%u";
const char *reserved2 = "";
const char *reserved3 = "";
bool use_sprintf = true;

enum Notation { STDFORMAT };

tostring_formatting() = default;
tostring_formatting(const char *int_fmt, const char *float_fmt = "%g",
Expand All @@ -498,12 +501,24 @@ struct tostring_formatting {
const char *array_end = "}", const char *array_sep = ",",
int flags = escape_strings,
const char *uint_fmt = "%u");

// Alternative ctr for std::format notation. You must pass STDFORMAT
// as the first argument.
tostring_formatting(Notation notation,
const char *int_fmt = "{}", const char *uint_fmt = "{}",
const char *float_fmt = "{}",
const char *string_fmt = "\"{}\"", const char *ptr_fmt = "{}",
const char *aggregate_begin = "(", const char *aggregate_end = ")",
const char *aggregate_sep = ",", const char *array_begin = "{",
const char *array_end = "}", const char *array_sep = ",",
int flags = escape_strings);
};



/// Return a string containing the data values formatted according
/// to the type and the optional formatting control arguments.
/// to the type and the optional formatting control arguments. Will be
/// deprecated someday as printf formatting falls out of favor.
OIIO_API std::string
tostring(TypeDesc type, const void* data, const tostring_formatting& fmt = {});

Expand Down
18 changes: 9 additions & 9 deletions src/libOpenImageIO/imagebuf.cpp
Expand Up @@ -108,7 +108,7 @@ class ImageBufImpl {
template<typename... Args>
void error(const char* fmt, const Args&... args) const
{
error(Strutil::sprintf(fmt, args...));
error(Strutil::fmt::format(fmt, args...));
}

template<typename... Args>
Expand Down Expand Up @@ -932,12 +932,12 @@ ImageBufImpl::read(int subimage, int miplevel, int chbegin, int chend,
auto input = ImageInput::open(m_name.string(), m_configspec.get(),
m_rioproxy);
if (!input) {
errorf("%s", OIIO::geterror());
error("{}", OIIO::geterror());
return false;
}
input->threads(threads()); // Pass on our thread policy
if (!input->read_native_deep_image(subimage, miplevel, m_deepdata)) {
errorf("%s", input->geterror());
error("{}", input->geterror());
return false;
}
m_spec = m_nativespec; // Deep images always use native data
Expand Down Expand Up @@ -1044,11 +1044,11 @@ ImageBufImpl::read(int subimage, int miplevel, int chbegin, int chend,
m_pixels_valid = true;
} else {
m_pixels_valid = false;
errorf("%s", in->geterror());
error("{}", in->geterror());
}
} else {
m_pixels_valid = false;
errorf("%s", OIIO::geterror());
error("{}", OIIO::geterror());
}
return m_pixels_valid;
}
Expand All @@ -1063,7 +1063,7 @@ ImageBufImpl::read(int subimage, int miplevel, int chbegin, int chend,
m_pixels_valid = true;
} else {
m_pixels_valid = false;
errorf("%s", m_imagecache->geterror());
error("{}", m_imagecache->geterror());
}

return m_pixels_valid;
Expand Down Expand Up @@ -1218,7 +1218,7 @@ ImageBuf::write(ImageOutput* out, ProgressCallback progress_callback,
}
}
if (!ok)
errorf("%s", out->geterror());
error("{}", out->geterror());
return ok;
}

Expand Down Expand Up @@ -1263,7 +1263,7 @@ ImageBuf::write(string_view _filename, TypeDesc dtype, string_view _fileformat,

auto out = ImageOutput::create(fileformat);
if (!out) {
errorf("%s", geterror());
error("{}", geterror());
return false;
}
out->threads(threads()); // Pass on our thread policy
Expand Down Expand Up @@ -1315,7 +1315,7 @@ ImageBuf::write(string_view _filename, TypeDesc dtype, string_view _fileformat,
}

if (!out->open(filename.c_str(), newspec)) {
errorf("%s", out->geterror());
error("{}", out->geterror());
return false;
}
if (!write(out.get(), progress_callback, progress_callback_data))
Expand Down
8 changes: 4 additions & 4 deletions src/libtexture/environment.cpp
Expand Up @@ -351,17 +351,17 @@ TextureSystemImpl::environment(TextureHandle* texture_handle_,
int s = m_imagecache->subimage_from_name(texturefile,
options.subimagename);
if (s < 0) {
errorf("Unknown subimage \"%s\" in texture \"%s\"",
options.subimagename, texturefile->filename());
error("Unknown subimage \"{}\" in texture \"{}\"",
options.subimagename, texturefile->filename());
return missing_texture(options, nchannels, result, dresultds,
dresultdt);
}
options.subimage = s;
options.subimagename.clear();
}
if (options.subimage < 0 || options.subimage >= texturefile->subimages()) {
errorf("Unknown subimage \"%s\" in texture \"%s\"",
options.subimagename, texturefile->filename());
error("Unknown subimage \"{}\" in texture \"{}\"", options.subimagename,
texturefile->filename());
return missing_texture(options, nchannels, result, dresultds,
dresultdt);
}
Expand Down