Skip to content

Commit 12075a5

Browse files
committed
#13126 RiaLogging: Add std::string_view overloads for log methods
Add error/warning/info/debug overloads accepting std::string and std::string_view, alongside the existing QString API. The new overloads are constrained via a StdStringLike concept so const char* / string literals do not match them and continue to bind to the QString overload unambiguously. The std::string_view overloads are now the canonical implementation: their inline bodies in the header carry the dedup, level filter, and logger fan-out logic. The QString overloads are reduced to thin forwarders that extract bytes via toUtf8() and dispatch into the templated overload. Internal helpers and sm_lastMessage migrate from QString to std::string_view / std::string. Encoding note: the QString forwarders now encode as UTF-8 instead of Latin-1 before reaching loggers. ASCII-only messages are unaffected; non-ASCII messages get correct UTF-8 byte sequences in log output. Twelve callsites that previously wrapped std::string in QString::fromStdString are simplified to call the new overload directly.
1 parent 93336ec commit 12075a5

12 files changed

Lines changed: 135 additions & 84 deletions

File tree

ApplicationLibCode/Application/RiaPreferencesGrid.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,13 @@ void RiaPreferencesGrid::setGridModelReaderOverride( const std::string& readerNa
310310
}
311311
else
312312
{
313-
RiaLogging::warning( QString::fromStdString( "Unknown EGRID reader type specified on command line: " + readerName ) );
313+
RiaLogging::warning( "Unknown EGRID reader type specified on command line: " + readerName );
314314
return;
315315
}
316316

317317
if ( readerType != RiaDefines::GridModelReader::NOT_SET )
318318
{
319-
RiaLogging::info( QString::fromStdString( "Using EGRID reader: " + readerName ) );
319+
RiaLogging::info( "Using EGRID reader: " + readerName );
320320
}
321321

322322
m_gridModelReaderOverride = readerType;

ApplicationLibCode/Application/Tools/RiaLogging.cpp

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,19 @@ void RiaDefaultConsoleLogger::writeToConsole( const std::string& str )
165165
//--------------------------------------------------------------------------------------------------
166166
///
167167
//--------------------------------------------------------------------------------------------------
168-
void RiaLogging::setLastMessage( const QString& message )
168+
void RiaLogging::setLastMessage( std::string_view message )
169169
{
170170
#pragma omp critical( critical_section_logging )
171171
{
172-
sm_lastMessage = message;
172+
sm_lastMessage.assign( message );
173173
sm_lastMessageTime = std::chrono::high_resolution_clock::now();
174174
}
175175
}
176176

177177
//--------------------------------------------------------------------------------------------------
178178
///
179179
//--------------------------------------------------------------------------------------------------
180-
bool RiaLogging::isSameMessage( const QString& message )
180+
bool RiaLogging::isSameMessage( std::string_view message )
181181
{
182182
bool isSame = false;
183183

@@ -198,14 +198,23 @@ bool RiaLogging::isSameMessage( const QString& message )
198198
return isSame;
199199
}
200200

201+
//--------------------------------------------------------------------------------------------------
202+
///
203+
//--------------------------------------------------------------------------------------------------
204+
bool RiaLogging::isKeywordEnabled( std::string_view keyword )
205+
{
206+
return RiaPreferencesSystem::current()->isLoggingActivatedForKeyword(
207+
QString::fromUtf8( keyword.data(), static_cast<qsizetype>( keyword.size() ) ) );
208+
}
209+
201210
//==================================================================================================
202211
//
203212
//
204213
//
205214
//==================================================================================================
206215

207216
std::vector<std::unique_ptr<RiaLogger>> RiaLogging::sm_logger;
208-
QString RiaLogging::sm_lastMessage;
217+
std::string RiaLogging::sm_lastMessage;
209218
std::chrono::time_point<std::chrono::high_resolution_clock> RiaLogging::sm_lastMessageTime;
210219

211220
//--------------------------------------------------------------------------------------------------
@@ -261,87 +270,48 @@ std::optional<RILogLevel> RiaLogging::parseLogLevelString( const QString& logLev
261270
}
262271

263272
//--------------------------------------------------------------------------------------------------
264-
///
273+
/// QString overloads forward to the templated StdStringLike overloads, which hold the canonical
274+
/// implementation. The QByteArray locals keep the underlying bytes alive for the call.
265275
//--------------------------------------------------------------------------------------------------
266-
void RiaLogging::error( const QString& message, const QString logKeyword )
276+
void RiaLogging::error( const QString& message, const QString& logKeyword )
267277
{
268-
if ( !RiaPreferencesSystem::current()->isLoggingActivatedForKeyword( logKeyword ) ) return;
269-
270-
if ( isSameMessage( message ) ) return;
271-
272-
for ( const auto& logger : sm_logger )
273-
{
274-
if ( logger && logger->level() >= int( RILogLevel::RI_LL_ERROR ) )
275-
{
276-
#pragma omp critical( critical_section_logging )
277-
logger->error( message.toLatin1().constData() );
278-
}
279-
}
280-
281-
setLastMessage( message );
278+
const auto msgUtf8 = message.toUtf8();
279+
const auto kwUtf8 = logKeyword.toUtf8();
280+
error( std::string_view{ msgUtf8.constData(), static_cast<size_t>( msgUtf8.size() ) },
281+
std::string_view{ kwUtf8.constData(), static_cast<size_t>( kwUtf8.size() ) } );
282282
}
283283

284284
//--------------------------------------------------------------------------------------------------
285285
///
286286
//--------------------------------------------------------------------------------------------------
287-
void RiaLogging::warning( const QString& message, const QString logKeyword )
287+
void RiaLogging::warning( const QString& message, const QString& logKeyword )
288288
{
289-
if ( !RiaPreferencesSystem::current()->isLoggingActivatedForKeyword( logKeyword ) ) return;
290-
291-
if ( isSameMessage( message ) ) return;
292-
293-
for ( const auto& logger : sm_logger )
294-
{
295-
if ( logger && logger->level() >= int( RILogLevel::RI_LL_WARNING ) )
296-
{
297-
#pragma omp critical( critical_section_logging )
298-
logger->warning( message.toLatin1().constData() );
299-
}
300-
}
301-
302-
setLastMessage( message );
289+
const auto msgUtf8 = message.toUtf8();
290+
const auto kwUtf8 = logKeyword.toUtf8();
291+
warning( std::string_view{ msgUtf8.constData(), static_cast<size_t>( msgUtf8.size() ) },
292+
std::string_view{ kwUtf8.constData(), static_cast<size_t>( kwUtf8.size() ) } );
303293
}
304294

305295
//--------------------------------------------------------------------------------------------------
306296
///
307297
//--------------------------------------------------------------------------------------------------
308-
void RiaLogging::info( const QString& message, const QString logKeyword )
298+
void RiaLogging::info( const QString& message, const QString& logKeyword )
309299
{
310-
if ( !RiaPreferencesSystem::current()->isLoggingActivatedForKeyword( logKeyword ) ) return;
311-
312-
if ( isSameMessage( message ) ) return;
313-
314-
for ( const auto& logger : sm_logger )
315-
{
316-
if ( logger && logger->level() >= int( RILogLevel::RI_LL_INFO ) )
317-
{
318-
#pragma omp critical( critical_section_logging )
319-
logger->info( message.toLatin1().constData() );
320-
}
321-
}
322-
323-
setLastMessage( message );
300+
const auto msgUtf8 = message.toUtf8();
301+
const auto kwUtf8 = logKeyword.toUtf8();
302+
info( std::string_view{ msgUtf8.constData(), static_cast<size_t>( msgUtf8.size() ) },
303+
std::string_view{ kwUtf8.constData(), static_cast<size_t>( kwUtf8.size() ) } );
324304
}
325305

326306
//--------------------------------------------------------------------------------------------------
327307
///
328308
//--------------------------------------------------------------------------------------------------
329-
void RiaLogging::debug( const QString& message, const QString logKeyword )
309+
void RiaLogging::debug( const QString& message, const QString& logKeyword )
330310
{
331-
if ( !RiaPreferencesSystem::current()->isLoggingActivatedForKeyword( logKeyword ) ) return;
332-
333-
if ( isSameMessage( message ) ) return;
334-
335-
for ( const auto& logger : sm_logger )
336-
{
337-
if ( logger && logger->level() >= int( RILogLevel::RI_LL_DEBUG ) )
338-
{
339-
#pragma omp critical( critical_section_logging )
340-
logger->debug( message.toLatin1().constData() );
341-
}
342-
}
343-
344-
setLastMessage( message );
311+
const auto msgUtf8 = message.toUtf8();
312+
const auto kwUtf8 = logKeyword.toUtf8();
313+
debug( std::string_view{ msgUtf8.constData(), static_cast<size_t>( msgUtf8.size() ) },
314+
std::string_view{ kwUtf8.constData(), static_cast<size_t>( kwUtf8.size() ) } );
345315
}
346316

347317
//--------------------------------------------------------------------------------------------------

ApplicationLibCode/Application/Tools/RiaLogging.h

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
#pragma once
2020

2121
#include <chrono>
22+
#include <concepts>
2223
#include <memory>
2324
#include <string>
2425
#include <string_view>
2526
#include <vector>
2627

2728
#include <QString>
2829

30+
template <typename T>
31+
concept StdStringLike = std::same_as<std::remove_cvref_t<T>, std::string> || std::same_as<std::remove_cvref_t<T>, std::string_view>;
32+
2933
enum class RILogLevel
3034
{
3135
RI_LL_DISABLED = 0,
@@ -68,21 +72,98 @@ class RiaLogging
6872
static RILogLevel logLevelBasedOnPreferences();
6973
static std::optional<RILogLevel> parseLogLevelString( const QString& logLevelString );
7074

71-
static void error( const QString& message, const QString logKeyword = "" );
72-
static void warning( const QString& message, const QString logKeyword = "" );
73-
static void info( const QString& message, const QString logKeyword = "" );
74-
static void debug( const QString& message, const QString logKeyword = "" );
75+
static void error( const QString& message, const QString& logKeyword = "" );
76+
static void warning( const QString& message, const QString& logKeyword = "" );
77+
static void info( const QString& message, const QString& logKeyword = "" );
78+
static void debug( const QString& message, const QString& logKeyword = "" );
79+
80+
template <StdStringLike Msg, StdStringLike Kw = std::string_view>
81+
static void error( const Msg& message, const Kw& logKeyword = std::string_view{} )
82+
{
83+
const std::string_view svMsg{ message };
84+
const std::string_view svKw{ logKeyword };
85+
if ( !isKeywordEnabled( svKw ) ) return;
86+
if ( isSameMessage( svMsg ) ) return;
87+
const std::string buf{ svMsg };
88+
for ( const auto& logger : sm_logger )
89+
{
90+
if ( logger && logger->level() >= int( RILogLevel::RI_LL_ERROR ) )
91+
{
92+
#pragma omp critical( critical_section_logging )
93+
logger->error( buf.c_str() );
94+
}
95+
}
96+
setLastMessage( svMsg );
97+
}
98+
99+
template <StdStringLike Msg, StdStringLike Kw = std::string_view>
100+
static void warning( const Msg& message, const Kw& logKeyword = std::string_view{} )
101+
{
102+
const std::string_view svMsg{ message };
103+
const std::string_view svKw{ logKeyword };
104+
if ( !isKeywordEnabled( svKw ) ) return;
105+
if ( isSameMessage( svMsg ) ) return;
106+
const std::string buf{ svMsg };
107+
for ( const auto& logger : sm_logger )
108+
{
109+
if ( logger && logger->level() >= int( RILogLevel::RI_LL_WARNING ) )
110+
{
111+
#pragma omp critical( critical_section_logging )
112+
logger->warning( buf.c_str() );
113+
}
114+
}
115+
setLastMessage( svMsg );
116+
}
117+
118+
template <StdStringLike Msg, StdStringLike Kw = std::string_view>
119+
static void info( const Msg& message, const Kw& logKeyword = std::string_view{} )
120+
{
121+
const std::string_view svMsg{ message };
122+
const std::string_view svKw{ logKeyword };
123+
if ( !isKeywordEnabled( svKw ) ) return;
124+
if ( isSameMessage( svMsg ) ) return;
125+
const std::string buf{ svMsg };
126+
for ( const auto& logger : sm_logger )
127+
{
128+
if ( logger && logger->level() >= int( RILogLevel::RI_LL_INFO ) )
129+
{
130+
#pragma omp critical( critical_section_logging )
131+
logger->info( buf.c_str() );
132+
}
133+
}
134+
setLastMessage( svMsg );
135+
}
136+
137+
template <StdStringLike Msg, StdStringLike Kw = std::string_view>
138+
static void debug( const Msg& message, const Kw& logKeyword = std::string_view{} )
139+
{
140+
const std::string_view svMsg{ message };
141+
const std::string_view svKw{ logKeyword };
142+
if ( !isKeywordEnabled( svKw ) ) return;
143+
if ( isSameMessage( svMsg ) ) return;
144+
const std::string buf{ svMsg };
145+
for ( const auto& logger : sm_logger )
146+
{
147+
if ( logger && logger->level() >= int( RILogLevel::RI_LL_DEBUG ) )
148+
{
149+
#pragma omp critical( critical_section_logging )
150+
logger->debug( buf.c_str() );
151+
}
152+
}
153+
setLastMessage( svMsg );
154+
}
75155

76156
static std::chrono::time_point<std::chrono::high_resolution_clock> currentTime();
77157
static void logElapsedTime( std::string_view message, const std::chrono::time_point<std::chrono::high_resolution_clock>& startTime );
78158

79159
private:
80-
static void setLastMessage( const QString& message );
81-
static bool isSameMessage( const QString& message );
160+
static void setLastMessage( std::string_view message );
161+
static bool isSameMessage( std::string_view message );
162+
static bool isKeywordEnabled( std::string_view keyword );
82163

83164
private:
84165
static std::vector<std::unique_ptr<RiaLogger>> sm_logger;
85-
static QString sm_lastMessage;
166+
static std::string sm_lastMessage;
86167
static std::chrono::time_point<std::chrono::high_resolution_clock> sm_lastMessageTime;
87168
};
88169

ApplicationLibCode/Commands/RicFaciesPropertiesImportTools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void RicFaciesPropertiesImportTools::importFaciesPropertiesFromFile( const QStri
5353
}
5454
catch ( RifRoffReaderException& ex )
5555
{
56-
RiaLogging::error( QString::fromStdString( ex.message ) );
56+
RiaLogging::error( ex.message );
5757
return;
5858
}
5959

ApplicationLibCode/FileInterface/RifEclipseInputFileTools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ bool RifEclipseInputFileTools::exportKeywords( const QString& resul
453453
auto result = extractKeywordData( eclipseCase, keyword, min, maxIn, refinement );
454454
if ( !result )
455455
{
456-
RiaLogging::warning( QString::fromStdString( result.error() ) );
456+
RiaLogging::warning( result.error() );
457457
continue;
458458
}
459459

ApplicationLibCode/FileInterface/RifOpmSummaryTools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ std::pair<std::set<RifEclipseSummaryAddress>, std::map<RifEclipseSummaryAddress,
118118
/*
119119
for ( const auto& kw : invalidKeywords )
120120
{
121-
RiaLogging::warning( QString::fromStdString( kw ) );
121+
RiaLogging::warning( kw );
122122
}
123123
*/
124124
}

ApplicationLibCode/GeoMech/GeoMechFileInterface/RifVtkReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ bool RifVtkReader::readFemParts( RigFemPartCollection* femParts )
130130
auto result = read( m_inputPath, parts, nodes, elements, elementSets, properties, m_displacements, m_stepNames );
131131
if ( !result )
132132
{
133-
RiaLogging::error( QString::fromStdString( result.error() ) );
133+
RiaLogging::error( result.error() );
134134
return false;
135135
}
136136

ApplicationLibCode/ProjectDataModel/Completions/RimMswSegmentCollection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void RimMswSegmentCollection::updateSegments( RimWellPath* topLevelWell, RimEcli
162162

163163
if ( !tableDataResult.has_value() )
164164
{
165-
RiaLogging::error( QString::fromStdString( tableDataResult.error() ) );
165+
RiaLogging::error( tableDataResult.error() );
166166
scheduleRedraw();
167167
return;
168168
}

ApplicationLibCode/ProjectDataModel/GeoMech/RimGeoMechCase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void RimGeoMechCase::reloadDataAndUpdate()
203203
std::string errMsg;
204204
if ( openGeoMechCase( &errMsg ) == CASE_OPEN_ERROR )
205205
{
206-
RiaLogging::error( QString::fromStdString( errMsg ) );
206+
RiaLogging::error( errMsg );
207207
}
208208
for ( auto& v : geoMechViews() )
209209
{

ApplicationLibCode/ProjectDataModel/Jobs/RimOpmFlowJob.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ int RimOpmFlowJob::mergeMswData( int mergePosition )
11071107
exportDate );
11081108
if ( !mswDataResult.has_value() )
11091109
{
1110-
RiaLogging::error( QString::fromStdString( mswDataResult.error() ) );
1110+
RiaLogging::error( mswDataResult.error() );
11111111
return failure;
11121112
}
11131113

0 commit comments

Comments
 (0)