/************************************************************************** Log player tech messages. **************************************************************************/ void TECH_LOG(int level, const struct player *pplayer, struct advance *padvance, const char *msg, ...) { char buffer[500]; char buffer2[500]; va_list ap; int minlevel = MIN(LOGLEVEL_TECH, level); if (!valid_advance(padvance) || advance_by_number(A_NONE) == padvance) { return; } if (BV_ISSET(pplayer->debug, PLAYER_DEBUG_TECH)) { minlevel = LOG_TEST; } else if (minlevel > fc_log_level) { return; } my_snprintf(buffer, sizeof(buffer), "%s::%s (want %d, dist %d) ", player_name(pplayer), advance_name_by_player(pplayer, advance_number(padvance)), pplayer->ai_data.tech_want[advance_index(padvance)], num_unknown_techs_for_goal(pplayer, advance_number(padvance))); va_start(ap, msg); my_vsnprintf(buffer2, sizeof(buffer2), msg, ap); va_end(ap); cat_snprintf(buffer, sizeof(buffer), "%s", buffer2); if (BV_ISSET(pplayer->debug, PLAYER_DEBUG_TECH)) { notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buffer); } freelog(minlevel, "%s", buffer); }
void my_error(int nr, myf MyFlags, ...) { const char *format; struct my_err_head *meh_p; va_list args; char ebuff[ERRMSGSIZE]; DBUG_ENTER("my_error"); DBUG_PRINT("my", ("nr: %d MyFlags: %d errno: %d", nr, MyFlags, errno)); /* Search for the error messages array, which could contain the message. */ for (meh_p= my_errmsgs_list; meh_p; meh_p= meh_p->meh_next) if (nr <= meh_p->meh_last) break; /* get the error message string. Default, if NULL or empty string (""). */ if (! (format= (meh_p && (nr >= meh_p->meh_first)) ? meh_p->meh_errmsgs[nr - meh_p->meh_first] : NULL) || ! *format) (void) my_snprintf(ebuff, sizeof(ebuff), "Unknown error %d", nr); else { va_start(args,MyFlags); (void) my_vsnprintf(ebuff, sizeof(ebuff), format, args); va_end(args); } (*error_handler_hook)(nr, ebuff, MyFlags); DBUG_VOID_RETURN; }
/************************************************************************** Log city messages, they will appear like this 2: Polish Romenna(5,35) [s1 d106 u11 g1] must have Archers ... **************************************************************************/ void CITY_LOG(int level, const struct city *pcity, const char *msg, ...) { char buffer[500]; char buffer2[500]; va_list ap; int minlevel = MIN(LOGLEVEL_CITY, level); if (pcity->debug) { minlevel = LOG_TEST; } else if (minlevel > fc_log_level) { return; } my_snprintf(buffer, sizeof(buffer), "%s %s(%d,%d) [s%d d%d u%d g%d] ", nation_rule_name(nation_of_city(pcity)), city_name(pcity), TILE_XY(pcity->tile), pcity->size, pcity->ai->danger, pcity->ai->urgency, pcity->ai->grave_danger); va_start(ap, msg); my_vsnprintf(buffer2, sizeof(buffer2), msg, ap); va_end(ap); cat_snprintf(buffer, sizeof(buffer), "%s", buffer2); if (pcity->debug) { notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buffer); } freelog(minlevel, "%s", buffer); }
int logger_vprintf(LOGGER_HANDLE *log, const char* fmt, va_list ap) { int result; my_off_t filesize; char cvtbuf[1024]; size_t n_bytes; mysql_mutex_lock(&log->lock); if (log->rotations > 0) if ((filesize= my_tell(log->file, MYF(0))) == (my_off_t) -1 || ((unsigned long long)filesize >= log->size_limit && do_rotate(log))) { result= -1; errno= my_errno; goto exit; /* Log rotation needed but failed */ } n_bytes= my_vsnprintf(cvtbuf, sizeof(cvtbuf), fmt, ap); if (n_bytes >= sizeof(cvtbuf)) n_bytes= sizeof(cvtbuf) - 1; result= my_write(log->file, (uchar *) cvtbuf, n_bytes, MYF(0)); exit: mysql_mutex_unlock(&log->lock); return result; }
int main(void) { char bufs[5] = { 'x', 'x', 'x', '\0', '\0' }; char bufd[5] = { 'x', 'x', 'x', '\0', '\0' }; int i; i = my_vsnprintf(bufs, "%s", "111"); if (strcmp(bufs, "1")) exit(1); if (i != 3) exit(1); i = my_vsnprintf(bufd, "%d", 111); if (strcmp(bufd, "1")) exit(1); if (i != 3) exit(1); exit(0); }
int my_snprintf(char* to, size_t n, const char* fmt, ...) { int result; va_list args; va_start(args,fmt); result= my_vsnprintf(to, n, fmt, args); va_end(args); return result; }
void Comment::comment_init(const char *format, va_list ap) { char buffer[BUFFER_SIZE]; size_t count = 0; count = my_vsnprintf(buffer, BUFFER_SIZE, format, ap); if( count > BUFFER_SIZE ) { //allocate larger char *newbuff = new char[count+1]; my_vsnprintf(newbuff,count+1,format,ap); //size_t newcount = my_vsnprintf(newbuff,count+1,format,ap); //assert(newcount == count); str = std::string(newbuff); delete[] newbuff; } else { str = std::string(buffer); } }
void my_printv_error(uint error, const char *format, myf MyFlags, va_list ap) { char ebuff[ERRMSGSIZE]; DBUG_ENTER("my_printv_error"); DBUG_PRINT("my", ("nr: %d MyFlags: %d errno: %d format: %s", error, MyFlags, errno, format)); (void) my_vsnprintf(ebuff, sizeof(ebuff), format, ap); (*error_handler_hook)(error, ebuff, MyFlags); DBUG_VOID_RETURN; }
void Ndb_move_data::set_error_code(int code, const char* fmt, ...) { va_list ap; va_start(ap, fmt); require(code != 0); Error& e = m_error; e.code = code; my_vsnprintf(e.message, sizeof(e.message), fmt, ap); va_end(ap); }
void my_printf_warning(const char *format, ...) { va_list args; char wbuff[ERRMSGSIZE]; DBUG_ENTER("my_printf_warning"); DBUG_PRINT("my", ("Format: %s", format)); va_start(args,format); (void) my_vsnprintf (wbuff, sizeof(wbuff), format, args); va_end(args); (*sql_print_warning_hook)(wbuff); DBUG_VOID_RETURN; }
/************************************************************************** Log unit messages, they will appear like this 2: Polish Archers[139] (5,35)->(0,0){0,0} stays to defend city where [] is unit id, ()->() are coordinates present and goto, and {,} contains bodyguard and ferryboat ids. **************************************************************************/ void UNIT_LOG(int level, const struct unit *punit, const char *msg, ...) { char buffer[500]; char buffer2[500]; va_list ap; int minlevel = MIN(LOGLEVEL_UNIT, level); int gx, gy; bool messwin = FALSE; /* output to message window */ if (punit->debug) { minlevel = LOG_TEST; } else { /* Are we a virtual unit evaluated in a debug city?. */ if (punit->id == 0) { struct city *pcity = tile_city(punit->tile); if (pcity && pcity->debug) { minlevel = LOG_TEST; messwin = TRUE; } } if (minlevel > fc_log_level) { return; } } if (punit->goto_tile) { gx = punit->goto_tile->x; gy = punit->goto_tile->y; } else { gx = gy = -1; } my_snprintf(buffer, sizeof(buffer), "%s %s[%d] %s (%d,%d)->(%d,%d){%d,%d} ", nation_rule_name(nation_of_unit(punit)), unit_rule_name(punit), punit->id, get_activity_text(punit->activity), TILE_XY(punit->tile), gx, gy, punit->ai.bodyguard, punit->ai.ferryboat); va_start(ap, msg); my_vsnprintf(buffer2, sizeof(buffer2), msg, ap); va_end(ap); cat_snprintf(buffer, sizeof(buffer), "%s", buffer2); if (punit->debug || messwin) { notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buffer); } freelog(minlevel, "%s", buffer); }
int my_printf_error(uint error, const char *format, myf MyFlags, ...) { va_list args; char ebuff[ERRMSGSIZE]; DBUG_ENTER("my_printf_error"); DBUG_PRINT("my", ("nr: %d MyFlags: %d errno: %d Format: %s", error, MyFlags, errno, format)); va_start(args,MyFlags); (void) my_vsnprintf (ebuff, sizeof(ebuff), format, args); va_end(args); DBUG_RETURN((*error_handler_hook)(error, ebuff, MyFlags)); }
/************************************************************************ Write to console and add line-break, and show prompt if required. ************************************************************************/ void con_write(enum rfc_status rfc_status, const char *message, ...) { /* First buffer contains featured text tags */ static char buf1[(MAX_LEN_CONSOLE_LINE * 3) / 2]; static char buf2[MAX_LEN_CONSOLE_LINE]; va_list args; va_start(args, message); my_vsnprintf(buf1, sizeof(buf1), message, args); va_end(args); /* remove all format tags */ featured_text_to_plain_text(buf1, buf2, sizeof(buf2), NULL); con_puts(rfc_status, buf2); }
/** Issue a message locally (i.e. on the same host the program is running on, don't transmit to a client). This is the default value for local_message_hook, and therefore the default printer for my_message_local(). mysys users should not call this directly, but go through my_message_local() instead. This printer prepends an Error/Warning/Note label to the string, then prints it to stderr using my_message_stderr(). Since my_message_stderr() appends a '\n', the format string should not end in a newline. @param ll log level: (ERROR|WARNING|INFORMATION)_LEVEL the printer may use these to filter for verbosity @param format a format string a la printf. Should not end in '\n' @param args parameters to go with that format string */ void my_message_local_stderr(enum loglevel ll, const char *format, va_list args) { char buff[1024]; size_t len; DBUG_ENTER("my_message_local_stderr"); len= my_snprintf(buff, sizeof(buff), "[%s] ", (ll == ERROR_LEVEL ? "ERROR" : ll == WARNING_LEVEL ? "Warning" : "Note")); my_vsnprintf(buff + len, sizeof(buff) - len, format, args); my_message_stderr(0, buff, MYF(0)); DBUG_VOID_RETURN; }
/**************************************************************************** Add the text to the string. ****************************************************************************/ static void vadd(struct astring *astr, const char *format, va_list ap) { size_t new_len; char buf[1024]; if (my_vsnprintf(buf, sizeof(buf), format, ap) == -1) { die("Formatted string bigger than %lu bytes", (unsigned long)sizeof(buf)); } /* Avoid calling strlen with NULL. */ astr_minsize(astr, 1); new_len = strlen(astr->str) + strlen(buf) + 1; astr_minsize(astr, new_len); mystrlcat(astr->str, buf, astr->n_alloc); }
int buffer_printf(char *buffer, int length, char *format, ...) { va_list vargs; int fulllen; va_start(vargs, format); #ifdef WIN32 fulllen = _vsnprintf(buffer, length, format, vargs); #else #ifdef NEED_SNPRINTF fulllen = my_vsnprintf(buffer, length, format, vargs); #else fulllen = vsnprintf(buffer, length, format, vargs); #endif #endif va_end(vargs); return fulllen; }
/************************************************************************ Write to console without line-break, don't print prompt. ************************************************************************/ static int con_dump(enum rfc_status rfc_status, const char *message, ...) { static char buf[MAX_LEN_CONSOLE_LINE]; va_list args; va_start(args, message); my_vsnprintf(buf, sizeof(buf), message, args); va_end(args); if (console_prompt_is_showing) { fc_printf("\n"); } if ((console_rfcstyle) && (rfc_status >= 0)) { fc_printf("%.3d %s", rfc_status, buf); } else { fc_printf("%s", buf); } console_prompt_is_showing = FALSE; return (int) strlen(buf); }
/************************************************************************** Log player messages, they will appear like this where ti is timer, co countdown and lo love for target, who is e. **************************************************************************/ void DIPLO_LOG(int level, const struct player *pplayer, const struct player *aplayer, const char *msg, ...) { char buffer[500]; char buffer2[500]; va_list ap; int minlevel = MIN(LOGLEVEL_PLAYER, level); const struct ai_dip_intel *adip; if (BV_ISSET(pplayer->debug, PLAYER_DEBUG_DIPLOMACY)) { minlevel = LOG_TEST; } else if (minlevel > fc_log_level) { return; } /* Don't use ai_data_get since it can have side effects. */ adip = ai_diplomacy_get(pplayer, aplayer); my_snprintf(buffer, sizeof(buffer), "%s->%s(l%d,c%d,d%d%s): ", player_name(pplayer), player_name(aplayer), pplayer->ai_data.love[player_index(aplayer)], adip->countdown, adip->distance, adip->is_allied_with_enemy ? "?" : (adip->at_war_with_ally ? "!" : "")); va_start(ap, msg); my_vsnprintf(buffer2, sizeof(buffer2), msg, ap); va_end(ap); cat_snprintf(buffer, sizeof(buffer), "%s", buffer2); if (BV_ISSET(pplayer->debug, PLAYER_DEBUG_DIPLOMACY)) { notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buffer); } freelog(minlevel, "%s", buffer); }
static int logg_internal(const enum loglevel level, const char *file, const char *func, const unsigned int line, int log_errno, const char *fmt, va_list args ) { struct big_buff *logg_buff; int ret_val, old_errno, retry_cnt; old_errno = errno; /* get our tls-print buf */ if(!(logg_buff = logg_get_buf())) { /* * hmmm, something went wrong, lets see, if we can get the message to * the user by other means */ return do_vlogging(level, fmt, args); } /* add time, if wanted, to buffer */ if(server.settings.logging.add_date_time) logg_buff->pos += add_time_to_buffer(buffer_start(*logg_buff), buffer_remaining(*logg_buff)); /* put out the "extra" stuff, if there */ if(file) { retry_cnt = 0; prefetch(strlpcpy); prefetch(file); prefetch(func); /* * calling snprintf for 2 * strcpy + an itoa is "nice" * but goes round the full stdio bloat: * snprintf->vsnprintf->vfprintf-> myriads of funcs to print */ do { char *sptr, *stmp, *rstart; size_t remaining; stmp = sptr = buffer_start(*logg_buff); remaining = buffer_remaining(*logg_buff); rstart = strlpcpy(sptr, file, remaining); remaining -= rstart - sptr; if(unlikely(remaining < 7)) goto realloc; sptr = rstart; *sptr++ = ':'; remaining--; rstart = strlpcpy(sptr, func, remaining); remaining -= rstart - sptr; if(unlikely(remaining < 18)) /* make sure we have enough space */ goto realloc; sptr = rstart; *sptr++ = '('; *sptr++ = ')'; *sptr++ = '@'; remaining -= 3; rstart = put_dec_trunc(sptr, line); /* 99,999 lines should be... */ strreverse(sptr, rstart - 1); remaining -= rstart - sptr; if(unlikely(remaining < 2)) goto realloc; sptr = rstart; *sptr++ = ':'; *sptr++ = ' '; logg_buff->pos += sptr - stmp; break; realloc: /* now we are in a slow path, no need to hurry */ { struct big_buff *tmp_buff; size_t len = strlen(file) + strlen(func) + 6 + 30 + strlen(fmt) * 3; len = ROUND_ALIGN(len * 2, 2048) + logg_buff->capacity; tmp_buff = realloc(logg_buff, sizeof(*logg_buff) + len); if(tmp_buff) { logg_buff = tmp_buff; logg_buff->limit = logg_buff->capacity = len; } else break; retry_cnt++; } } while(retry_cnt < 4); } ret_val = 0; retry_cnt = 0; /* format the message in tls */ do { size_t len = logg_buff->capacity; va_list tmp_valist; if(ret_val < 0) { len *= 2; if(++retry_cnt > 4) { ret_val = 0; break; } } else if((size_t)ret_val > buffer_remaining(*logg_buff)) len = ROUND_ALIGN((size_t)ret_val * 2, 1024); /* align to a k */ if(unlikely(len != logg_buff->capacity)) { struct big_buff *tmp_buf = realloc(logg_buff, sizeof(*logg_buff) + len); if(tmp_buf) { logg_buff = tmp_buf; logg_buff->limit = logg_buff->capacity = len; } else { ret_val = buffer_remaining(*logg_buff); break; } } /* put msg printed out in buffer */ va_copy(tmp_valist, args); ret_val = my_vsnprintf(buffer_start(*logg_buff), buffer_remaining(*logg_buff), fmt, tmp_valist); va_end(tmp_valist); /* error? repeat */ } while(unlikely(ret_val < 0 || (size_t)ret_val > buffer_remaining(*logg_buff))); logg_buff->pos += (size_t)ret_val; /* add errno string if wanted */ if(log_errno) { if(buffer_remaining(*logg_buff) < STRERROR_R_SIZE + 4) { size_t len = logg_buff->capacity * 2; struct big_buff *tmp_buff = realloc(logg_buff, sizeof(*logg_buff) + len); if(!tmp_buff) goto no_errno; logg_buff = tmp_buff; logg_buff->limit = logg_buff->capacity += len; } *buffer_start(*logg_buff) = ':'; logg_buff->pos++; *buffer_start(*logg_buff) = ' '; logg_buff->pos++; { #if defined STRERROR_R_CHAR_P || defined HAVE_MTSAFE_STRERROR || WIN32 || !(defined HAVE_STRERROR_R || HAVE_DECL_STRERROR_R-0 > 0) size_t err_str_len; # ifdef WIN32 const char *s = buffer_start(*logg_buff); if(!(err_str_len = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, /* flags */ 0, /* pointer to other source */ old_errno, /* msg id */ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* language */ buffer_start(*logg_buff), /* buffer */ buffer_remaining(*logg_buff)-1, /* size */ 0 /* va_args */ ))) { s = "Unknown system error"; err_str_len = strlen(s) < buffer_remaining(*logg_buff)-2 ? strlen(s) : buffer_remaining(*logg_buff)-2; } # else # ifdef STRERROR_R_CHAR_P /* * the f***ing GNU-Version of strerror_r wich returns * a char * to the buffer.... * This sucks especially in conjunction with strnlen, * wich needs a #define __GNU_SOURCE, but conflicts * with this... */ const char *s = strerror_r(old_errno, buffer_start(*logg_buff), buffer_remaining(*logg_buff)-2); # else /* * Ol Solaris seems to have a static msgtable, so * strerror is threadsafe and we don't have a * _r version */ /* * we also simply fall into here if strerror is not thread * safe, but we have nothing else. * Since what should we do in this case... _sys_errlist * is a bsd extentions. */ const char *s = strerror(old_errno); # endif if(s) err_str_len = strnlen(s, buffer_remaining(*logg_buff)-2); else { s = "Unknown system error"; err_str_len = strlen(s) < (buffer_remaining(*logg_buff)-2) ? strlen(s) : buffer_remaining(*logg_buff)-2; } # endif if(s != buffer_start(*logg_buff)) my_memcpy(buffer_start(*logg_buff), s, err_str_len); logg_buff->pos += err_str_len; #else if(!strerror_r(old_errno, buffer_start(*logg_buff), buffer_remaining(*logg_buff))) // if(!strerror_s(buffer_start(*logg_buff), buffer_remaining(*logg_buff), old_errno)) logg_buff->pos += strnlen(buffer_start(*logg_buff), buffer_remaining(*logg_buff)); else { size_t err_l; const char *bs; if(EINVAL == errno) { err_l = str_size("Unknown errno value!"); bs = "Unknown errno value!"; } else if(ERANGE == errno) { err_l = str_size("errno msg to long for buffer!"); bs = "errno msg to long for buffer!"; } else { err_l = str_size("failure while retrieving errno msg!"); bs = "failure while retrieving errno msg!"; } err_l = (buffer_remaining(*logg_buff)-2) >= err_l ? err_l : (buffer_remaining(*logg_buff)-2); my_memcpy(buffer_start(*logg_buff), bs, err_l); logg_buff->pos += err_l; } #endif } *buffer_start(*logg_buff) = '\n'; logg_buff->pos++; *buffer_start(*logg_buff) = '\0'; } no_errno: /* output that stuff */ buffer_flip(*logg_buff); ret_val = do_logging(level, buffer_start(*logg_buff), buffer_remaining(*logg_buff)); logg_ret_buf(logg_buff); return ret_val; }