tests: extract stdout redirection
[platform/core/system/dlog.git] / tests / test_libredirect_multi.c
1 /* DLOG
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <dlog.h>
21 #include <dlog-redirect-stdout.h>
22
23 int main()
24 {
25         /* We send two messages on the main buffer and one on the system buffer.
26          * Each message has a different priority and tag. This allows us to make sure
27          * all the parameters can be changed by reconnecting. Additionally,
28          * sending two of the messages on the same buffer allows us to check
29          * if the message order is correct. */
30
31         /* NB: The flushes are needed here. This is because stdout is buffered.
32          * If we don't flush, it will only actually do the write syscalls at the end
33          * of the program execution. Therefore, all the logs will be written to the
34          * last connection. Note that libdlog tries to mitigate that, but this binary
35          * is not linked to libdlog, and the mitigation is a best-effort hack anyway. */
36         if (dlog_connect_fd(LOG_ID_MAIN, STDOUT_FILENO, "TAG1", DLOG_WARN) < 0)
37                 return EXIT_FAILURE;
38         printf("Message 1");
39         fflush(stdout);
40         if (dlog_connect_fd(LOG_ID_MAIN, STDOUT_FILENO, "TAG2", DLOG_ERROR) < 0)
41                 return EXIT_FAILURE;
42         printf("Message 2");
43         fflush(stdout);
44         if (dlog_connect_fd(LOG_ID_SYSTEM, STDOUT_FILENO, "TAG3", DLOG_INFO) < 0)
45                 return EXIT_FAILURE;
46         printf("Message 3");
47         fflush(stdout);
48 }