Skip to content

Commit

Permalink
changes to nowcsrtomb and entry.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
rmknan committed Oct 7, 2023
2 parents 35ee4d8 + c54a498 commit dc7e079
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ set(STUMPLESS_SOURCES
${PROJECT_SOURCE_DIR}/src/target/stream.c
${PROJECT_SOURCE_DIR}/src/version.c
${PROJECT_SOURCE_DIR}/src/validate.c
${PROJECT_SOURCE_DIR}/src/priority.c
)


Expand Down Expand Up @@ -645,6 +646,7 @@ install(FILES
${PROJECT_SOURCE_DIR}/include/stumpless/severity.h
${PROJECT_SOURCE_DIR}/include/stumpless/target.h
${PROJECT_SOURCE_DIR}/include/stumpless/version.h
${PROJECT_SOURCE_DIR}/include/stumpless/priority.h
DESTINATION "include/stumpless"
)

Expand Down Expand Up @@ -957,6 +959,10 @@ add_function_test(version
SOURCES test/function/version.cpp
)

add_function_test(priority
SOURCES test/function/priority.cpp
)

add_custom_target(build-test
DEPENDS ${STUMPLESS_FUNCTION_TESTS}
)
Expand Down
1 change: 1 addition & 0 deletions include/stumpless.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
#include <stumpless/target/function.h>
#include <stumpless/target/stream.h>
#include <stumpless/version.h>
#include <stumpless/priority.h>

#ifdef STUMPLESS_JOURNALD_TARGETS_SUPPORTED
/** @example journald_example.c
Expand Down
66 changes: 66 additions & 0 deletions include/stumpless/priority.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* SPDX-License-Identifier: Apache-2.0 */

/*
* Copyright 2022 Joel E. Anderson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/** @file
* Priority value (PRIVAL) represents both the Facility and Severity values.
*
* @since release v2.2.0
*/

#ifndef __STUMPLESS_PRIORITY_H
# define __STUMPLESS_PRIORITY_H

# include <stumpless.h>

# ifdef __cplusplus
extern "C" {
# endif

/**
* Extract PRIVAL number (Facility and Severity) from the given string with
* the direct number or with two names divided with a period in the order:
* facility first, then severity ("<facility_descr>.<severity_descr>").
*
* **Thread Safety: MT-Safe**
* This function is thread safe. A mutex is used to coordinate changes to the
* target while it is being read.
*
* **Async Signal Safety: AS-Unsafe lock**
* This function is not safe to call from signal handlers due to the use of a
* non-reentrant lock to coordinate the read of the target.
*
* **Async Cancel Safety: AC-Unsafe lock**
* This function is not safe to call from threads that may be asynchronously
* cancelled, due to the use of a lock that could be left locked..
*
* @since release v2.2.0
*
* @param string The string to extract the prival from.
*
* @return the PRIVAL number used for the severity and facility values of
* the logged entry, in the event of an error it returns -1.
*/
STUMPLESS_PUBLIC_FUNCTION
int
stumpless_prival_from_string( const char *string );

# ifdef __cplusplus
} /* extern "C" */
# endif

#endif /* __STUMPLESS_PRIORITY_H */
32 changes: 16 additions & 16 deletions src/config/no_wcsrtombs_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@ no_wcsrtombs_s_copy_wstring_to_cstring( const wchar_t *str, int *copy_size ) {
goto fail;
}

buffer_size = conversion_result + 1; // add NULL terminator
buffer = alloc_mem( buffer_size );
if( !buffer ) {
goto fail;
}
// buffer_size = conversion_result + 1; // add NULL terminator
// buffer = alloc_mem( buffer_size );
// if( !buffer ) {
// goto fail;
// }

conversion_result = wcsrtombs( buffer, &str, buffer_size, &state );
if( conversion_result == -1 ) {
raise_wide_conversion_failure( errno, L10N_ERRNO_ERROR_CODE_TYPE );
goto cleanup_and_fail;
}
// conversion_result = wcsrtombs( buffer, &str, buffer_size, &state );
// if( conversion_result == -1 ) {
// raise_wide_conversion_failure( errno, L10N_ERRNO_ERROR_CODE_TYPE );
// goto cleanup_and_fail;
// }

if( copy_size ) {
*copy_size = cap_size_t_to_int( buffer_size );
}
// if( copy_size ) {
// *copy_size = cap_size_t_to_int( buffer_size );
// }

return buffer;
// return buffer;

cleanup_and_fail:
free_mem( buffer );
// cleanup_and_fail:
// free_mem( buffer );
fail:
return NULL;
}
118 changes: 118 additions & 0 deletions src/priority.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// SPDX-License-Identifier: Apache-2.0

/*
* Copyright 2022 Joel E. Anderson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <stumpless.h>
#include "private/memory.h"
#include "private/entry.h"
#include "private/validate.h"
#include "private/config.h"

static void toUpperCase( char *str ) {
for( int i = 0; str[i]; i++) {
str[i] = toupper( str[i] );
}
}

int
stumpless_prival_from_string( const char *string ) {
int prival;
int severity;
int facility;
char *param;
char *period;
char *sec_period;
size_t len;
size_t slen;
size_t pre_len;
const char pre_facility[] = "STUMPLESS_FACILITY_";
const char pre_severity[] = "STUMPLESS_SEVERITY_";

VALIDATE_ARG_NOT_NULL_INT_RETURN( string );

if( unlikely( !string[0] ) ) {
return -1;
}

slen = strlen( string );

if( isdigit( string[0] ) ) {
prival = atoi( string );
if( prival >= 0 && prival <= 191 && slen < 4 ) {
return prival;
}
}

// find the first period character
period = strchr( string, '.' );
if( !period )
return -1;

// check there is no another period character
sec_period = strchr( period + 1, '.' );
if( sec_period != NULL ) {
return -1;
}

// Calculate the facility length, up to the first period character
len = period - string;
pre_len = sizeof(pre_facility) - 1;
param = alloc_mem( len + 1 + pre_len );
if( !param ) {
return -1;
}
// Copy the facility substring to the param buffer
memcpy( param, pre_facility, pre_len );
memcpy( param + pre_len, string, len );
param[pre_len + len] = '\0';

toUpperCase(param);
facility = stumpless_get_facility_enum( param );

free_mem( param );

if( facility < 0 )
return -1;

// Calculate the severity length
len = slen - ++len;
pre_len = sizeof(pre_severity) - 1;

param = alloc_mem( len + 1 + pre_len );
if( !param ) {
return -1;
}
// Copy the severity substring to the param buffer
memcpy( param, pre_severity, pre_len );
memcpy( param + pre_len, ++period, len);
param[pre_len + len] = '\0';

toUpperCase(param);
severity = stumpless_get_severity_enum( param );

free_mem( param );

if( severity < 0 )
return -1;

return get_prival( facility, severity );
}

6 changes: 5 additions & 1 deletion src/windows/stumpless.def
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,8 @@ EXPORTS
stumpless_unload_entry_only @206
stumpless_unload_param @207
vstumpless_load_entry @208
stumpless_set_entry_message_str_w @209
<<<<<<< HEAD
stumpless_set_entry_message_str_w @210
=======
stumpless_prival_from_string @209
>>>>>>> c54a4983dc4a72f12029ef0c6004a947afbde6d7
4 changes: 4 additions & 0 deletions test/function/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ Tests for general target functionality.

## `version.cpp`
Tests for versioning of the library.


## `priority.cpp`
Tests for functions that deal with priority values (PRIVAL).
17 changes: 9 additions & 8 deletions test/function/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdlib.h>
#include <string.h>
#include <stumpless.h>
#include <locale.h>
#include "test/helper/assert.hpp"
#include "test/helper/fixture.hpp"
#include "test/helper/memory_allocation.hpp"
Expand Down Expand Up @@ -2511,20 +2512,22 @@ namespace {
stumpless_free_all( );
}

TEST( SetMessageWideStrTest, Utf16Message ) {
TEST( SetMessageWideStrTest, wide_message ) {
setlocale(LC_ALL, "");
struct stumpless_entry *entry;
const struct stumpless_entry *result;
const wchar_t *utf16_message= L"没有错误";
const wchar_t *wide_message= L"没有错误";
const char *utf8_message = "没有错误";
const char *new_message;


entry = create_empty_entry( );
ASSERT_NOT_NULL( entry );


ASSERT_NOT_NULL( wide_message );

// utf16_message = load_corpus( "cstring/zh-cn" );
ASSERT_NOT_NULL( utf16_message );

result = stumpless_set_entry_message_str_w( entry, utf16_message );
result = stumpless_set_entry_message_str_w( entry, wide_message );
EXPECT_EQ( entry, result );
EXPECT_NO_ERROR;

Expand All @@ -2533,8 +2536,6 @@ namespace {
EXPECT_NO_ERROR;
EXPECT_STREQ( utf8_message, new_message );

delete[] utf16_message;
free( ( void * ) new_message );
stumpless_destroy_entry_and_contents( entry );
stumpless_free_all( );
}
Expand Down
Loading

0 comments on commit dc7e079

Please sign in to comment.