Skip to content

Commit 989e5be

Browse files
committed
Removing printf from Svc. Partially Fixed #1708
1 parent 464f4ea commit 989e5be

File tree

17 files changed

+128
-197
lines changed

17 files changed

+128
-197
lines changed

Drv/LinuxSpiDriver/LinuxSpiDriverComponentImpl.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <Drv/LinuxSpiDriver/LinuxSpiDriverComponentImpl.hpp>
1414
#include <FpConfig.hpp>
1515
#include <Fw/Types/Assert.hpp>
16-
#include <Fw/Types/StringUtils.hpp>
16+
#include <Fw/Types/FileNameString.hpp>
1717
#include <cstdint>
1818
#include <unistd.h>
1919
#include <cstdio>
@@ -76,10 +76,11 @@ namespace Drv {
7676
NATIVE_INT_TYPE ret;
7777

7878
// Open:
79-
char devName[256];
80-
Fw::StringUtils::format(devName, sizeof devName, "/dev/spidev%d.%d", device, select);
79+
Fw::FileNameString devString;
80+
Fw::FormatStatus formatStatus = devString.format("/dev/spidev%d.%d", device, select);
81+
FW_ASSERT(formatStatus == Fw::FormatStatus::SUCCESS);
8182

82-
fd = ::open(devName, O_RDWR);
83+
fd = ::open(devString.toChar(), O_RDWR);
8384
if (fd == -1) {
8485
this->log_WARNING_HI_SPI_OpenError(device,select,fd);
8586
return false;

Fw/Types/StringBase.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,45 @@ bool StringBase::operator==(const CHAR* other) const {
5353
return (result == 0);
5454
}
5555

56-
void StringBase::format(const CHAR* formatString, ...) {
56+
FormatStatus StringBase::format(const CHAR* formatString, ...) {
5757
va_list args;
5858
va_start(args, formatString);
59-
this->vformat(formatString, args);
59+
FormatStatus status = this->vformat(formatString, args);
6060
va_end(args);
61+
return status;
6162
}
6263

63-
void StringBase::vformat(const CHAR* formatString, va_list args) {
64+
FormatStatus StringBase::vformat(const CHAR* formatString, va_list args) {
6465
CHAR* us = const_cast<CHAR*>(this->toChar());
6566
SizeType cap = this->getCapacity();
6667
FW_ASSERT(us != nullptr);
67-
FW_ASSERT(formatString != nullptr);
68+
69+
// Check format string
70+
if (formatString == nullptr) {
71+
return FormatStatus::INVALID_FORMAT_STRING;
72+
}
73+
FwSizeType total_needed_size = 0;
6874
#if FW_USE_PRINTF_FAMILY_FUNCTIONS_IN_STRING_FORMATTING
69-
(void) vsnprintf(us, cap, formatString, args);
75+
// Check that the API size type fits in fprime size type
76+
static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<PlatformIntType>::max(),
77+
"Range of PlarformIntType does not fit within range of FwSizeType");
78+
PlatformIntType total_needed_size_api = vsnprintf(us, cap, formatString, args);
79+
// Check for error return, or a type overflow
80+
if (total_needed_size_api < 0) {
81+
return FormatStatus::OTHER_ERROR;
82+
}
83+
total_needed_size = static_cast<FwSizeType>(total_needed_size_api);
7084
#else
85+
total_needed_size = StringUtils::string_length(format_string, cap);
7186
*this = formatString;
7287
#endif
7388
// Force null terminate
7489
us[cap - 1] = 0;
90+
// Check for overflow
91+
if (total_needed_size >= cap) {
92+
return FormatStatus::OVERFLOW;
93+
}
94+
return FormatStatus::SUCCESS;
7595
}
7696

7797
bool StringBase::operator!=(const StringBase& other) const {

Fw/Types/StringBase.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
#endif
2222

2323
namespace Fw {
24+
25+
//! \brief status of format
26+
enum class FormatStatus {
27+
SUCCESS, //!< Format worked
28+
OVERFLOW, //!< Format overflowed
29+
INVALID_FORMAT_STRING, //!< Format provided invalid format string
30+
OTHER_ERROR //!< An error was returned from an underlying call
31+
};
32+
2433
class StringBase : public Serializable {
2534
public:
2635
using SizeType = NATIVE_UINT_TYPE;
@@ -62,8 +71,8 @@ class StringBase : public Serializable {
6271
StringBase& operator=(const CHAR* src); //!< Assign CHAR*
6372
StringBase& operator=(const StringBase& src); //!< Assign another StringBase
6473

65-
void format(const CHAR* formatString, ...); //!< write formatted string to buffer
66-
void vformat(const CHAR* formatString, va_list args); //!< write formatted string to buffer using va_list
74+
FormatStatus format(const CHAR* formatString, ...); //!< write formatted string to buffer
75+
FormatStatus vformat(const CHAR* formatString, va_list args); //!< write formatted string to buffer using va_list
6776

6877
virtual SerializeStatus serialize(SerializeBufferBase& buffer) const; //!< serialization function
6978
virtual SerializeStatus serialize(SerializeBufferBase& buffer, SizeType maxLen) const; //!< serialization function

Fw/Types/StringUtils.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,3 @@ FwSignedSizeType Fw::StringUtils::substring_find(const CHAR* source_string,
7373
// if we make it here, no matches were found
7474
return -1;
7575
}
76-
77-
void Fw::StringUtils::format(CHAR* destination, Fw::StringBase::SizeType size, const CHAR* format, ...) {
78-
va_list args;
79-
va_start(args, format);
80-
Fw::ExternalString(destination, size).vformat(format, args);
81-
va_end(args);
82-
}

Fw/Types/StringUtils.hpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
/**
2+
* Fw/Types/StringUtils.hpp:
3+
*
4+
* C-string helper utilities. Note: wherever possible, use Fw::StringBase and derived classes instead of raw C-strings.
5+
*/
16
#ifndef FW_STRINGUTILS_HPP
27
#define FW_STRINGUTILS_HPP
38
#include <FpConfig.hpp>
4-
#include <Fw/Types/StringBase.hpp>
59

610
namespace Fw {
711
namespace StringUtils {
@@ -45,21 +49,6 @@ FwSizeType string_length(const CHAR* source, FwSizeType buffer_size);
4549
*/
4650
FwSignedSizeType substring_find(const CHAR* source_string, FwSizeType source_size, const CHAR* sub_string, FwSizeType sub_size);
4751

48-
//! \brief format a string and store into a destination buffer
49-
//!
50-
//! This function will format a string into a destination buffer. The destination buffer must be large enough to hold
51-
//! the formatted string. The format delegates to the standard C library function vsnprintf underneath the hood.
52-
//!
53-
//! \warning if used in execution (i.e. a file path), assert FW_USE_PRINTF_FAMILY_FUNCTIONS_IN_STRING_FORMATTING.
54-
//! ```
55-
//! static_assert(FW_USE_PRINTF_FAMILY_FUNCTIONS_IN_STRING_FORMATTING, "String formatting disabled")
56-
//! ```
57-
//! \param destination: destination buffer to hold formatted string
58-
//! \param size: size of the destination buffer. Output will be truncated to this length
59-
//! \param format: constant format string
60-
//! \param ...: variable arguments to to pass to the format string
61-
void format(CHAR* destination, StringBase::SizeType size, const CHAR* format, ...);
62-
6352
enum StringToNumberStatus {
6453
SUCCESSFUL_CONVERSION, //!< Output should be valid
6554
NULL_INPUT, //!< A null string was supplied

Svc/ActiveTextLogger/ActiveTextLogger.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,14 @@ namespace Svc {
7272
severityString = "SEVERITY ERROR";
7373
break;
7474
}
75-
76-
// TODO: Add calling task id to format string
77-
char textStr[FW_INTERNAL_INTERFACE_STRING_MAX_SIZE];
78-
79-
(void) snprintf(textStr,
80-
FW_INTERNAL_INTERFACE_STRING_MAX_SIZE,
81-
"EVENT: (%" PRI_FwEventIdType ") (%" PRI_FwTimeBaseStoreType ":%" PRIu32 ",%" PRIu32 ") %s: %s\n",
82-
id, static_cast<FwTimeBaseStoreType>(timeTag.getTimeBase()), timeTag.getSeconds(), timeTag.getUSeconds(),
83-
severityString, text.toChar());
75+
// Overflow is allowed and truncation accepted
76+
Fw::InternalInterfaceString intText;
77+
(void) intText.format("EVENT: (%" PRI_FwEventIdType ") (%" PRI_FwTimeBaseStoreType ":%" PRIu32 ",%" PRIu32 ") %s: %s\n",
78+
id, static_cast<FwTimeBaseStoreType>(timeTag.getTimeBase()), timeTag.getSeconds(), timeTag.getUSeconds(),
79+
severityString, text.toChar());
8480

8581
// Call internal interface so that everything else is done on component thread,
8682
// this helps ensure consistent ordering of the printed text:
87-
Fw::InternalInterfaceString intText(textStr);
8883
this->TextQueue_internalInterfaceInvoke(intText);
8984
}
9085

Svc/ActiveTextLogger/LogFile.cpp

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -87,60 +87,40 @@ namespace Svc {
8787
this->m_openFile = false;
8888
this->m_file.close();
8989
}
90+
Fw::FileNameString searchFilename;
91+
Fw::FormatStatus formatStatus = searchFilename.format("%s", fileName);
9092

9193
// If file name is too large, return failure:
92-
FwSizeType fileNameSize = Fw::StringUtils::string_length(fileName, static_cast<FwSizeType>(Fw::String::STRING_SIZE));
93-
if (fileNameSize == Fw::String::STRING_SIZE) {
94+
if (formatStatus != Fw::FormatStatus::SUCCESS) {
9495
return false;
9596
}
9697

97-
U32 suffix = 0;
98-
FwSignedSizeType tmp;
99-
char fileNameFinal[Fw::String::STRING_SIZE];
100-
(void) strncpy(fileNameFinal,fileName,
101-
Fw::String::STRING_SIZE);
102-
fileNameFinal[Fw::String::STRING_SIZE-1] = 0;
103-
10498
// Check if file already exists, and if it does try to tack on a suffix.
105-
// Quit after 10 suffix addition tries (first try is w/ the original name).
99+
// Quit after maxBackups suffix addition tries (first try is w/ the original name).
100+
U32 suffix = 0;
106101
bool failedSuffix = false;
107-
while (Os::FileSystem::getFileSize(fileNameFinal,tmp) == Os::FileSystem::OP_OK) {
108-
109-
// If the file name was the max size, then can't append a suffix,
110-
// so just fail:
111-
if (fileNameSize == (Fw::String::STRING_SIZE-1)) {
112-
return false;
113-
}
114-
102+
FwSignedSizeType fileSize = 0;
103+
while (Os::FileSystem::getFileSize(searchFilename.toChar(), fileSize) == Os::FileSystem::OP_OK) {
115104
// Not able to create a new non-existing file in maxBackups tries, then mark that it failed:
116105
if (suffix >= maxBackups) {
117106
failedSuffix = true;
118107
break;
119108
}
120-
121-
NATIVE_INT_TYPE stat = snprintf(fileNameFinal,Fw::String::STRING_SIZE,
122-
"%s%" PRIu32,fileName,suffix);
123-
124-
// If there was error, then just fail:
125-
if (stat <= 0) {
109+
// Format and check for error and overflows
110+
formatStatus = searchFilename.format("%s%" PRIu32, fileName, suffix);
111+
if (formatStatus != Fw::FormatStatus::SUCCESS) {
126112
return false;
127113
}
128-
129-
// There should never be truncation:
130-
FW_ASSERT(stat < Fw::String::STRING_SIZE);
131-
132114
++suffix;
133115
}
134116

135117
// If failed trying to make a new file, just use the original file
136118
if (failedSuffix) {
137-
(void) strncpy(fileNameFinal,fileName,
138-
Fw::String::STRING_SIZE);
139-
fileNameFinal[Fw::String::STRING_SIZE-1] = 0;
119+
searchFilename = fileName;
140120
}
141121

142122
// Open the file (using CREATE so that it truncates an already existing file):
143-
Os::File::Status stat = this->m_file.open(fileNameFinal, Os::File::OPEN_CREATE, Os::File::OverwriteType::OVERWRITE);
123+
Os::File::Status stat = this->m_file.open(searchFilename.toChar(), Os::File::OPEN_CREATE, Os::File::OverwriteType::OVERWRITE);
144124

145125
// Bad status when trying to open the file:
146126
if (stat != Os::File::OP_OK) {
@@ -149,7 +129,7 @@ namespace Svc {
149129

150130
this->m_currentFileSize = 0;
151131
this->m_maxFileSize = maxSize;
152-
this->m_fileName = fileNameFinal;
132+
this->m_fileName = searchFilename;
153133
this->m_openFile = true;
154134

155135
return true;

Svc/ActiveTextLogger/LogFile.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#ifndef SVCLOGFILE_HPP_
77
#define SVCLOGFILE_HPP_
88

9-
#include <Fw/Types/String.hpp>
9+
#include <Fw/Types/FileNameString.hpp>
1010
#include <Os/File.hpp>
1111
#include <Os/FileSystem.hpp>
1212

@@ -55,7 +55,7 @@ namespace Svc {
5555
// ----------------------------------------------------------------------
5656

5757
// The name of the file to text logs to:
58-
Fw::String m_fileName;
58+
Fw::FileNameString m_fileName;
5959

6060
// The file to write text logs to:
6161
Os::File m_file;

Svc/ActiveTextLogger/test/ut/ActiveTextLoggerTester.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,13 @@ namespace Svc {
253253

254254
printf("Testing file name larger than string size\n");
255255

256-
// Setup filename larger than 80 char:
257-
char longFileName[Fw::String::STRING_SIZE + 1];
258-
for (U32 i = 0; i < Fw::String::STRING_SIZE; ++i) {
256+
// Setup filename larger than file name string can accept
257+
// Maximum valid file name is Fw::FileNameString::STRING_SIZE, add 1 to be too long, and an extra for the \0
258+
char longFileName[Fw::FileNameString::STRING_SIZE + 2];
259+
for (U32 i = 0; i < Fw::FileNameString::STRING_SIZE + 1; ++i) {
259260
longFileName[i] = 'a';
260261
}
261-
longFileName[Fw::String::STRING_SIZE] = 0;
262-
262+
longFileName[Fw::FileNameString::STRING_SIZE + 1] = 0;
263263
stat = this->component.set_log_file(longFileName,50);
264264

265265
// Verify file not made:
@@ -269,11 +269,12 @@ namespace Svc {
269269
Os::FileSystem::getFileSize(longFileName,tmp));
270270

271271
printf("Testing file name of max size and file already exists\n");
272-
char longFileNameDup[Fw::String::STRING_SIZE];
273-
for (U32 i = 0; i < Fw::String::STRING_SIZE; ++i) {
272+
// Maximum valid file name is Fw::FileNameString::STRING_SIZE add one for \0
273+
char longFileNameDup[Fw::FileNameString::STRING_SIZE + 1];
274+
for (U32 i = 0; i < Fw::FileNameString::STRING_SIZE; ++i) {
274275
longFileNameDup[i] = 'a';
275276
}
276-
longFileNameDup[Fw::String::STRING_SIZE-1] = 0;
277+
longFileNameDup[Fw::FileNameString::STRING_SIZE] = 0;
277278

278279
stat = this->component.set_log_file(longFileNameDup,50);
279280

@@ -311,9 +312,6 @@ namespace Svc {
311312
char baseNameWithSuffix[128];
312313
U32 i;
313314
for (i = 0; i < 10; ++i) {
314-
315-
//printf("<< %i\n",i);
316-
317315
stat = this->component.set_log_file(baseName,50);
318316

319317
snprintf(baseNameWithSuffix, sizeof(baseNameWithSuffix), "%s%d",baseName,i);

Svc/AssertFatalAdapter/AssertFatalAdapterComponentImpl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ namespace Svc {
118118

119119
CHAR msg[Fw::StringBase::BUFFER_SIZE(FW_ASSERT_TEXT_SIZE)] = {0};
120120
Fw::defaultReportAssert(file,lineNo,numArgs,arg1,arg2,arg3,arg4,arg5,arg6,msg,sizeof(msg));
121-
// fprintf(stderr... allocates large buffers on stack as stderr is unbuffered by the OS
122-
// and this can conflict with the traditionally smaller stack sizes.
123-
printf("%s\n", msg);
121+
Fw::Logger::log("%s\n", msg);
124122

125123
// Handle the case where the ports aren't connected yet
126124
if (not this->isConnected_Log_OutputPort(0)) {

0 commit comments

Comments
 (0)