1 /*  RetroArch - A frontend for libretro.
2  *  Copyright (C) 2010-2014 - Hans-Kristian Arntzen
3  *  Copyright (C) 2011-2017 - Daniel De Matteis
4  *
5  *  RetroArch is free software: you can redistribute it and/or modify it under the terms
6  *  of the GNU General Public License as published by the Free Software Found-
7  *  ation, either version 3 of the License, or (at your option) any later version.
8  *
9  *  RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  *  PURPOSE.  See the GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License along with RetroArch.
14  *  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #if defined(__PSL1GHT__) || defined(__PS3__)
18 #include <defines/ps3_defines.h>
19 #endif
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 
26 #include <retro_miscellaneous.h>
27 #include <net/net_compat.h>
28 #include <net/net_socket.h>
29 
30 #include "../verbosity.h"
31 
32 #if !defined(PC_DEVELOPMENT_IP_ADDRESS)
33 #error "An IP address for the PC logging server was not set in the Makefile, cannot continue."
34 #endif
35 
36 #if !defined(PC_DEVELOPMENT_UDP_PORT)
37 #error "An UDP port for the PC logging server was not set in the Makefile, cannot continue."
38 #endif
39 
40 /* TODO/FIXME - static global variables */
41 static int g_sid;
42 static struct sockaddr_in target;
43 
logger_init(void)44 void logger_init(void)
45 {
46    socket_target_t in_target;
47    const char *server = PC_DEVELOPMENT_IP_ADDRESS;
48    unsigned      port = PC_DEVELOPMENT_UDP_PORT;
49 
50    if (!network_init())
51    {
52       printf("Could not initialize network logger interface.\n");
53       return;
54    }
55 
56    g_sid  = socket_create(
57          "ra_netlogger",
58          SOCKET_DOMAIN_INET,
59          SOCKET_TYPE_DATAGRAM,
60          SOCKET_PROTOCOL_NONE);
61 
62    in_target.port   = port;
63    in_target.server = server;
64    in_target.domain = SOCKET_DOMAIN_INET;
65 
66    socket_set_target(&target, &in_target);
67 }
68 
logger_shutdown(void)69 void logger_shutdown(void)
70 {
71    if (socket_close(g_sid) < 0)
72       printf("Could not close socket.\n");
73 
74    network_deinit();
75 }
76 
logger_send(const char * __format,...)77 void logger_send(const char *__format,...)
78 {
79    va_list args;
80 
81    va_start(args,__format);
82    logger_send_v(__format, args);
83    va_end(args);
84 }
85 
logger_send_v(const char * __format,va_list args)86 void logger_send_v(const char *__format, va_list args)
87 {
88    static char sendbuf[4096];
89    int len;
90 
91    vsnprintf(sendbuf,4000,__format, args);
92    len = strlen(sendbuf);
93 
94    sendto(g_sid,
95          sendbuf,
96          len,
97          MSG_DONTWAIT,
98          (struct sockaddr*)&target,
99          sizeof(target));
100 }
101