9 #include <sys/syscall.h>
14 #include <sys/domain.h>
15 #include <xtypes/domain.h>
16 #include <xseg/domain.h>
18 #include <xtypes/xlock.h>
20 int (*xseg_snprintf)(char *str, size_t size, const char *format, ...) = snprintf;
22 char __xseg_errbuf[4096];
24 static struct xlock __lock = { .owner = Noone};
26 void __lock_domain(void)
28 (void)xlock_acquire(&__lock, 1);
31 void __unlock_domain(void)
33 xlock_release(&__lock);
36 void __load_plugin(const char *name)
41 unsigned int namelen = strlen(name);
43 strncpy(_name, "xseg_", 5);
44 strncpy(_name + 5, name, 80);
45 strncpy(_name + 5 + namelen, ".so", 3);
46 _name[5 + namelen + 3 ] = 0;
47 dl = dlopen(_name, RTLD_NOW);
49 XSEGLOG("Cannot load plugin '%s': %s\n", _name, dlerror());
53 strncpy(_name + 5 + namelen, "_init", 5);
55 init = (void (*)(void))(long)dlsym(dl, _name);
57 XSEGLOG("Init function '%s' not found!\n", _name);
62 //XSEGLOG("Plugin '%s' loaded.\n", name);
65 uint64_t __get_id(void)
67 return (uint64_t)syscall(SYS_gettid);
70 void __xseg_log(const char *msg)
76 void *xtypes_malloc(unsigned long size)
81 void xtypes_free(void *ptr)
86 void __get_current_time(struct timeval *tv) {
87 gettimeofday(tv, NULL);
90 int __renew_logctx(struct log_ctx *lc, char *peer_name,
91 enum log_level log_level, char *logfile, uint32_t flags)
96 strncpy(lc->peer_name, peer_name, MAX_PEER_NAME);
97 lc->peer_name[MAX_PEER_NAME -1] = 0;
100 lc->log_level = log_level;
101 if (logfile && logfile[0]) {
102 strncpy(lc->filename, logfile, MAX_LOGFILE_LEN);
103 lc->filename[MAX_LOGFILE_LEN - 1] = 0;
105 else if (!(flags & REOPEN_FILE) || lc->logfile == stderr)
108 if (lc->logfile == stderr)
109 file = fopen(lc->filename, "a");
111 file = freopen(lc->filename, "a", lc->logfile);
116 flags &= ~REOPEN_FILE;
117 if ((flags|lc->flags) & REDIRECT_STDOUT)
118 if (!freopen(lc->filename, "a", stdout))
120 if ((flags|lc->flags) & REDIRECT_STDERR)
121 if (!freopen(lc->filename, "a", stderr))
127 int (*renew_logctx)(struct log_ctx *lc, char *peer_name,
128 enum log_level log_level, char *logfile, uint32_t flags) = __renew_logctx;
130 int __init_logctx(struct log_ctx *lc, char *peer_name,
131 enum log_level log_level, char *logfile, uint32_t flags)
136 strncpy(lc->peer_name, peer_name, MAX_PEER_NAME);
137 lc->peer_name[MAX_PEER_NAME -1] = 0;
143 lc->log_level = log_level;
144 if (!logfile || !logfile[0]) {
145 lc->logfile = stderr;
149 strncpy(lc->filename, logfile, MAX_LOGFILE_LEN);
150 lc->filename[MAX_LOGFILE_LEN - 1] = 0;
151 file = fopen(lc->filename, "a");
153 lc->logfile = stderr;
158 if (flags & REDIRECT_STDOUT)
159 if (!freopen(lc->filename, "a", stdout))
161 if (flags & REDIRECT_STDERR)
162 if (!freopen(lc->filename, "a", stderr))
168 int (*init_logctx)(struct log_ctx *lc, char *peer_name,
169 enum log_level log_level, char *logfile, uint32_t flags) = __init_logctx;
171 void __xseg_log2(struct log_ctx *lc, enum log_level level, char *fmt, ...)
175 char timebuf[1024], buffer[4096];
177 char *t = NULL, *pn = NULL;
181 case E: t = "XSEG[EE]"; break;
182 case W: t = "XSEG[WW]"; break;
183 case I: t = "XSEG[II]"; break;
184 case D: t = "XSEG[DD]"; break;
185 default: t = "XSEG[UNKNONW]"; break;
189 pn = "Invalid peer name";
192 ctime_r(&timeval, timebuf);
193 *strchr(timebuf, '\n') = '\0';
195 buf += sprintf(buf, "%s: ", t);
196 buf += snprintf(buf, MAX_PEER_NAME + 2, "%s: ", lc->peer_name);
197 buf += sprintf(buf, "%s (%ld):\n\t", timebuf, timeval);
198 unsigned long rem = sizeof(buffer) - (buf - buffer);
199 buf += vsnprintf(buf, rem, fmt, ap);
200 if (buf >= buffer + sizeof(buffer))
201 buf = buffer + sizeof(buffer) - 2;/* enough to hold \n and \0 */
202 buf += sprintf(buf, "\n");
204 fprintf(lc->logfile, "%s", buffer);
211 void xseg_printtrace(void)
218 size = backtrace (array, 10);
219 strings = backtrace_symbols (array, size);
221 XSEGLOG("Obtained %zd stack frames.\n", size);
223 for (i = 0; i < size; i++)
224 XSEGLOG ("%s\n", strings[i]);