summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorHaru <haru@dotalux.com>2015-06-18 02:29:43 +0200
committerHaru <haru@dotalux.com>2015-08-13 16:27:31 +0200
commit6080c7c97a32d3ac28466138a52873ed184f278d (patch)
tree035fb430668c42b6ab7dce644e9315635792e690 /src/common
parenteecdcc208c5a80d9d85e51e1a7b155b6a18c6e91 (diff)
downloadhercules-6080c7c97a32d3ac28466138a52873ed184f278d.tar.gz
hercules-6080c7c97a32d3ac28466138a52873ed184f278d.tar.bz2
hercules-6080c7c97a32d3ac28466138a52873ed184f278d.tar.xz
hercules-6080c7c97a32d3ac28466138a52873ed184f278d.zip
Added showmsg HPM interface
- The showmsg interface is automatically imported into plugins by the HPM (just like previously, the various Show* functions were). This change requires no actions from plugin developers. - stdout_with_ansisequence is now available through showmsg->stdout_with_ansisequence - msg_silent is now available through showmsg->silent - console_msg_log is now available through showmsg->console_log - timestamp_format is now available through showmsg->timestamp_format - Plugin-safe macros are provided, so that all Show* and Clear* calls will require no changes. - vShowMessage is provided through the public API, as va_list variant of ShowMessage. - vShowMessage_ is no longer part of the public API. If necessary, va_list variants of the other Show* functions will be added at a later time as follow-ups. Signed-off-by: Haru <haru@dotalux.com>
Diffstat (limited to 'src/common')
-rw-r--r--src/common/HPM.c12
-rw-r--r--src/common/HPMDataCheck.h5
-rw-r--r--src/common/HPMi.h4
-rw-r--r--src/common/core.c7
-rw-r--r--src/common/showmsg.c149
-rw-r--r--src/common/showmsg.h75
6 files changed, 155 insertions, 97 deletions
diff --git a/src/common/HPM.c b/src/common/HPM.c
index cdd9bb769..caf0f9471 100644
--- a/src/common/HPM.c
+++ b/src/common/HPM.c
@@ -100,15 +100,7 @@ bool hplugin_populate(struct hplugin *plugin, const char *filename) {
const char* name;
void *Ref;
} ToLink[] = {
- HPM_POP(ShowMessage),
- HPM_POP(ShowStatus),
- HPM_POP(ShowSQL),
- HPM_POP(ShowInfo),
- HPM_POP(ShowNotice),
- HPM_POP(ShowWarning),
- HPM_POP(ShowDebug),
- HPM_POP(ShowError),
- HPM_POP(ShowFatalError),
+ HPM_POP(showmsg),
};
int i, length = ARRAYLENGTH(ToLink);
@@ -778,6 +770,8 @@ void hplugins_share_defaults(void) {
HPM->share(DB, "DB");
HPM->share(HPMiMalloc, "iMalloc");
HPM->share(nullpo,"nullpo");
+ /* showmsg */
+ HPM->share(showmsg,"showmsg");
/* socket */
HPM->share(sockt,"sockt");
/* strlib */
diff --git a/src/common/HPMDataCheck.h b/src/common/HPMDataCheck.h
index 584cab5c9..f7b1fcb98 100644
--- a/src/common/HPMDataCheck.h
+++ b/src/common/HPMDataCheck.h
@@ -185,6 +185,11 @@ HPExport const struct s_HPMDataCheck HPMDataCheck[] = {
#else
#define COMMON_NULLPO_H
#endif // COMMON_NULLPO_H
+ #ifdef COMMON_SHOWMSG_H
+ { "showmsg_interface", sizeof(struct showmsg_interface), SERVER_TYPE_ALL },
+ #else
+ #define COMMON_SHOWMSG_H
+ #endif // COMMON_SHOWMSG_H
#ifdef COMMON_SOCKET_H
{ "hSockOpt", sizeof(struct hSockOpt), SERVER_TYPE_ALL },
{ "s_subnet", sizeof(struct s_subnet), SERVER_TYPE_ALL },
diff --git a/src/common/HPMi.h b/src/common/HPMi.h
index bf4eb83c8..2054657be 100644
--- a/src/common/HPMi.h
+++ b/src/common/HPMi.h
@@ -7,6 +7,7 @@
#include "common/cbasetypes.h"
#include "common/console.h"
#include "common/core.h"
+#include "common/showmsg.h"
#include "common/sql.h"
struct script_state;
@@ -20,9 +21,6 @@ struct map_session_data;
#define HPExport
#endif
-/* after */
-#include "common/showmsg.h"
-
#define HPM_VERSION "1.0"
#define HPM_ADDCONF_LENGTH 40
diff --git a/src/common/core.c b/src/common/core.c
index e663c4e4c..e46c9589b 100644
--- a/src/common/core.c
+++ b/src/common/core.c
@@ -176,6 +176,7 @@ void core_defaults(void) {
console_defaults();
strlib_defaults();
malloc_defaults();
+ showmsg_defaults();
cmdline_defaults();
#ifndef MINICORE
libconfig_defaults();
@@ -317,7 +318,7 @@ int cmdline_exec(int argc, char **argv, unsigned int options)
}
if (options&CMDLINE_OPT_SILENT) {
if (data->options&CMDLINE_OPT_SILENT) {
- msg_silent = 0x7; // silence information and status messages
+ showmsg->silent = 0x7; // silence information and status messages
break;
}
} else if ((data->options&CMDLINE_OPT_PREINIT) == (options&CMDLINE_OPT_PREINIT)) {
@@ -393,6 +394,7 @@ int main (int argc, char **argv) {
core_defaults();
iMalloc->init();// needed for Show* in display_title() [FlavioJS]
+ showmsg->init();
cmdline->init();
@@ -402,7 +404,7 @@ int main (int argc, char **argv) {
sysinfo->init();
- if (!(msg_silent&0x1))
+ if (!(showmsg->silent&0x1))
console->display_title();
usercheck();
@@ -458,6 +460,7 @@ int main (int argc, char **argv) {
//sysinfo->final(); Called by iMalloc->final()
iMalloc->final();
+ showmsg->final(); // Should be after iMalloc->final()
return retval;
}
diff --git a/src/common/showmsg.c b/src/common/showmsg.c
index 01dc0b01a..68af31917 100644
--- a/src/common/showmsg.c
+++ b/src/common/showmsg.c
@@ -30,16 +30,7 @@
#define DEBUGLOGPATH "log"PATHSEP_STR"login-server.log"
#endif
-///////////////////////////////////////////////////////////////////////////////
-/// behavioral parameter.
-/// when redirecting output:
-/// if true prints escape sequences
-/// if false removes the escape sequences
-int stdout_with_ansisequence = 0;
-
-int msg_silent = 0; //Specifies how silent the console is.
-
-int console_msg_log = 0;//[Ind] msg error logging
+struct showmsg_interface showmsg_s;
///////////////////////////////////////////////////////////////////////////////
/// static/dynamic buffer for the messages
@@ -197,8 +188,7 @@ int VFPRINTF(HANDLE handle, const char *fmt, va_list argptr)
// Print everything to the buffer
BUFVPRINTF(tempbuf,fmt,argptr);
- if( !is_console(handle) && stdout_with_ansisequence )
- {
+ if (!is_console(handle) && showmsg->stdout_with_ansisequence) {
WriteFile(handle, BUFVAL(tempbuf), BUFLEN(tempbuf), &written, 0);
return 0;
}
@@ -490,8 +480,7 @@ int VFPRINTF(FILE *file, const char *fmt, va_list argptr)
if(!fmt || !*fmt)
return 0;
- if( is_console(file) || stdout_with_ansisequence )
- {
+ if (is_console(file) || showmsg->stdout_with_ansisequence) {
vfprintf(file, fmt, argptr);
return 0;
}
@@ -596,9 +585,6 @@ int FPRINTF(FILE *file, const char *fmt, ...) {
#endif// not _WIN32
-
-char timestamp_format[20] = ""; //For displaying Timestamps
-
int vShowMessage_(enum msg_type flag, const char *string, va_list ap)
{
va_list apcopy;
@@ -612,9 +598,9 @@ int vShowMessage_(enum msg_type flag, const char *string, va_list ap)
return 1;
}
if(
- ( flag == MSG_WARNING && console_msg_log&1 ) ||
- ( ( flag == MSG_ERROR || flag == MSG_SQL ) && console_msg_log&2 ) ||
- ( flag == MSG_DEBUG && console_msg_log&4 ) ) {//[Ind]
+ ( flag == MSG_WARNING && showmsg->console_log&1 ) ||
+ ( ( flag == MSG_ERROR || flag == MSG_SQL ) && showmsg->console_log&2 ) ||
+ ( flag == MSG_DEBUG && showmsg->console_log&4 ) ) {//[Ind]
FILE *log = NULL;
if( (log = fopen(SERVER_TYPE == SERVER_TYPE_MAP ? "./log/map-msg_log.log" : "./log/unknown.log","a+")) ) {
char timestring[255];
@@ -635,20 +621,20 @@ int vShowMessage_(enum msg_type flag, const char *string, va_list ap)
}
}
if(
- (flag == MSG_INFORMATION && msg_silent&1) ||
- (flag == MSG_STATUS && msg_silent&2) ||
- (flag == MSG_NOTICE && msg_silent&4) ||
- (flag == MSG_WARNING && msg_silent&8) ||
- (flag == MSG_ERROR && msg_silent&16) ||
- (flag == MSG_SQL && msg_silent&16) ||
- (flag == MSG_DEBUG && msg_silent&32)
+ (flag == MSG_INFORMATION && showmsg->silent&1) ||
+ (flag == MSG_STATUS && showmsg->silent&2) ||
+ (flag == MSG_NOTICE && showmsg->silent&4) ||
+ (flag == MSG_WARNING && showmsg->silent&8) ||
+ (flag == MSG_ERROR && showmsg->silent&16) ||
+ (flag == MSG_SQL && showmsg->silent&16) ||
+ (flag == MSG_DEBUG && showmsg->silent&32)
)
return 0; //Do not print it.
- if (timestamp_format[0] && flag != MSG_NONE) {
+ if (showmsg->timestamp_format[0] && flag != MSG_NONE) {
//Display time format. [Skotlex]
time_t t = time(NULL);
- strftime(prefix, 80, timestamp_format, localtime(&t));
+ strftime(prefix, 80, showmsg->timestamp_format, localtime(&t));
} else prefix[0]='\0';
switch (flag) {
@@ -721,7 +707,17 @@ int vShowMessage_(enum msg_type flag, const char *string, va_list ap)
return 0;
}
-void ClearScreen(void)
+int showmsg_vShowMessage(const char *string, va_list ap)
+{
+ int ret;
+ va_list apcopy;
+ va_copy(apcopy, ap);
+ ret = vShowMessage_(MSG_NONE, string, apcopy);
+ va_end(apcopy);
+ return ret;
+}
+
+void showmsg_clearScreen(void)
{
#ifndef _WIN32
ShowMessage(CL_CLS); // to prevent empty string passed messages
@@ -738,50 +734,57 @@ int ShowMessage_(enum msg_type flag, const char *string, ...) {
}
// direct printf replacement
-void ShowMessage(const char *string, ...) __attribute__((format(printf, 1, 2)));
-void ShowMessage(const char *string, ...) {
+void showmsg_showMessage(const char *string, ...) __attribute__((format(printf, 1, 2)));
+void showmsg_showMessage(const char *string, ...)
+{
va_list ap;
va_start(ap, string);
vShowMessage_(MSG_NONE, string, ap);
va_end(ap);
}
-void ShowStatus(const char *string, ...) __attribute__((format(printf, 1, 2)));
-void ShowStatus(const char *string, ...) {
+void showmsg_showStatus(const char *string, ...) __attribute__((format(printf, 1, 2)));
+void showmsg_showStatus(const char *string, ...)
+{
va_list ap;
va_start(ap, string);
vShowMessage_(MSG_STATUS, string, ap);
va_end(ap);
}
-void ShowSQL(const char *string, ...) __attribute__((format(printf, 1, 2)));
-void ShowSQL(const char *string, ...) {
+void showmsg_showSQL(const char *string, ...) __attribute__((format(printf, 1, 2)));
+void showmsg_showSQL(const char *string, ...)
+{
va_list ap;
va_start(ap, string);
vShowMessage_(MSG_SQL, string, ap);
va_end(ap);
}
-void ShowInfo(const char *string, ...) __attribute__((format(printf, 1, 2)));
-void ShowInfo(const char *string, ...) {
+void showmsg_showInfo(const char *string, ...) __attribute__((format(printf, 1, 2)));
+void showmsg_showInfo(const char *string, ...)
+{
va_list ap;
va_start(ap, string);
vShowMessage_(MSG_INFORMATION, string, ap);
va_end(ap);
}
-void ShowNotice(const char *string, ...) __attribute__((format(printf, 1, 2)));
-void ShowNotice(const char *string, ...) {
+void showmsg_showNotice(const char *string, ...) __attribute__((format(printf, 1, 2)));
+void showmsg_showNotice(const char *string, ...)
+{
va_list ap;
va_start(ap, string);
vShowMessage_(MSG_NOTICE, string, ap);
va_end(ap);
}
-void ShowWarning(const char *string, ...) __attribute__((format(printf, 1, 2)));
-void ShowWarning(const char *string, ...) {
+void showmsg_showWarning(const char *string, ...) __attribute__((format(printf, 1, 2)));
+void showmsg_showWarning(const char *string, ...)
+{
va_list ap;
va_start(ap, string);
vShowMessage_(MSG_WARNING, string, ap);
va_end(ap);
}
-void ShowConfigWarning(config_setting_t *config, const char *string, ...) __attribute__((format(printf, 2, 3)));
-void ShowConfigWarning(config_setting_t *config, const char *string, ...) {
+void showmsg_showConfigWarning(config_setting_t *config, const char *string, ...) __attribute__((format(printf, 2, 3)));
+void showmsg_showConfigWarning(config_setting_t *config, const char *string, ...)
+{
StringBuf buf;
va_list ap;
StrBuf->Init(&buf);
@@ -792,24 +795,70 @@ void ShowConfigWarning(config_setting_t *config, const char *string, ...) {
va_end(ap);
StrBuf->Destroy(&buf);
}
-void ShowDebug(const char *string, ...) __attribute__((format(printf, 1, 2)));
-void ShowDebug(const char *string, ...) {
+void showmsg_showDebug(const char *string, ...) __attribute__((format(printf, 1, 2)));
+void showmsg_showDebug(const char *string, ...)
+{
va_list ap;
va_start(ap, string);
vShowMessage_(MSG_DEBUG, string, ap);
va_end(ap);
}
-void ShowError(const char *string, ...) __attribute__((format(printf, 1, 2)));
-void ShowError(const char *string, ...) {
+void showmsg_showError(const char *string, ...) __attribute__((format(printf, 1, 2)));
+void showmsg_showError(const char *string, ...)
+{
va_list ap;
va_start(ap, string);
vShowMessage_(MSG_ERROR, string, ap);
va_end(ap);
}
-void ShowFatalError(const char *string, ...) __attribute__((format(printf, 1, 2)));
-void ShowFatalError(const char *string, ...) {
+void showmsg_showFatalError(const char *string, ...) __attribute__((format(printf, 1, 2)));
+void showmsg_showFatalError(const char *string, ...)
+{
va_list ap;
va_start(ap, string);
vShowMessage_(MSG_FATALERROR, string, ap);
va_end(ap);
}
+
+void showmsg_init(void)
+{
+}
+
+void showmsg_final(void)
+{
+}
+
+void showmsg_defaults(void)
+{
+ showmsg = &showmsg_s;
+
+ ///////////////////////////////////////////////////////////////////////////////
+ /// behavioral parameter.
+ /// when redirecting output:
+ /// if true prints escape sequences
+ /// if false removes the escape sequences
+ showmsg->stdout_with_ansisequence = false;
+
+ showmsg->silent = 0; //Specifies how silent the console is.
+
+ showmsg->console_log = 0;//[Ind] msg error logging
+
+ memset(showmsg->timestamp_format, '\0', sizeof(showmsg->timestamp_format));
+
+ showmsg->init = showmsg_init;
+ showmsg->final = showmsg_final;
+
+ showmsg->clearScreen = showmsg_clearScreen;
+ showmsg->showMessageV = showmsg_vShowMessage;
+
+ showmsg->showMessage = showmsg_showMessage;
+ showmsg->showStatus = showmsg_showStatus;
+ showmsg->showSQL = showmsg_showSQL;
+ showmsg->showInfo = showmsg_showInfo;
+ showmsg->showNotice = showmsg_showNotice;
+ showmsg->showWarning = showmsg_showWarning;
+ showmsg->showDebug = showmsg_showDebug;
+ showmsg->showError = showmsg_showError;
+ showmsg->showFatalError = showmsg_showFatalError;
+ showmsg->showConfigWarning = showmsg_showConfigWarning;
+}
diff --git a/src/common/showmsg.h b/src/common/showmsg.h
index 63e42ab57..6b119a1ce 100644
--- a/src/common/showmsg.h
+++ b/src/common/showmsg.h
@@ -7,11 +7,7 @@
#include "common/cbasetypes.h"
-#ifdef HERCULES_CORE
-# include <libconfig/libconfig.h>
-#else
-# include "common/HPMi.h"
-#endif
+#include <libconfig/libconfig.h>
#include <stdarg.h>
@@ -85,35 +81,48 @@ enum msg_type {
MSG_FATALERROR
};
+struct showmsg_interface {
+ bool stdout_with_ansisequence; //If the color ANSI sequences are to be used. [flaviojs]
+ int silent; //Specifies how silent the console is. [Skotlex]
+ int console_log; //Specifies what error messages to log. [Ind]
+ char timestamp_format[20]; //For displaying Timestamps [Skotlex]
+
+ void (*init) (void);
+ void (*final) (void);
+
+ void (*clearScreen) (void);
+ int (*showMessageV) (const char *string, va_list ap);
+
+ void (*showMessage) (const char *, ...) __attribute__((format(printf, 1, 2)));
+ void (*showStatus) (const char *, ...) __attribute__((format(printf, 1, 2)));
+ void (*showSQL) (const char *, ...) __attribute__((format(printf, 1, 2)));
+ void (*showInfo) (const char *, ...) __attribute__((format(printf, 1, 2)));
+ void (*showNotice) (const char *, ...) __attribute__((format(printf, 1, 2)));
+ void (*showWarning) (const char *, ...) __attribute__((format(printf, 1, 2)));
+ void (*showDebug) (const char *, ...) __attribute__((format(printf, 1, 2)));
+ void (*showError) (const char *, ...) __attribute__((format(printf, 1, 2)));
+ void (*showFatalError) (const char *, ...) __attribute__((format(printf, 1, 2)));
+ void (*showConfigWarning) (config_setting_t *config, const char *string, ...) __attribute__((format(printf, 2, 3)));
+};
+
+struct showmsg_interface *showmsg;
+
+/* the purpose of these macros is simply to not make calling them be an annoyance */
+#define ClearScreen() (showMsg->clearScreen())
+#define vShowMessage(fmt, list) (showmsg->showMessageV((fmt), (list)))
+#define ShowMessage(fmt, ...) (showmsg->showMessage((fmt), ##__VA_ARGS__))
+#define ShowStatus(fmt, ...) (showmsg->showStatus((fmt), ##__VA_ARGS__))
+#define ShowSQL(fmt, ...) (showmsg->showSQL((fmt), ##__VA_ARGS__))
+#define ShowInfo(fmt, ...) (showmsg->showInfo((fmt), ##__VA_ARGS__))
+#define ShowNotice(fmt, ...) (showmsg->showNotice((fmt), ##__VA_ARGS__))
+#define ShowWarning(fmt, ...) (showmsg->showWarning((fmt), ##__VA_ARGS__))
+#define ShowDebug(fmt, ...) (showmsg->showDebug((fmt), ##__VA_ARGS__))
+#define ShowError(fmt, ...) (showmsg->showError((fmt), ##__VA_ARGS__))
+#define ShowFatalError(fmt, ...) (showmsg->showFatalError((fmt), ##__VA_ARGS__))
+#define ShowConfigWarning(config, fmt, ...) (showmsg->showConfigWarning((config), (fmt), ##__VA_ARGS__))
+
#ifdef HERCULES_CORE
-extern int stdout_with_ansisequence; //If the color ANSI sequences are to be used. [flaviojs]
-extern int msg_silent; //Specifies how silent the console is. [Skotlex]
-extern int console_msg_log; //Specifies what error messages to log. [Ind]
-extern char timestamp_format[20]; //For displaying Timestamps [Skotlex]
-
-extern void ClearScreen(void);
-extern int vShowMessage_(enum msg_type flag, const char *string, va_list ap);
-
- extern void ShowMessage(const char *, ...) __attribute__((format(printf, 1, 2)));
- extern void ShowStatus(const char *, ...) __attribute__((format(printf, 1, 2)));
- extern void ShowSQL(const char *, ...) __attribute__((format(printf, 1, 2)));
- extern void ShowInfo(const char *, ...) __attribute__((format(printf, 1, 2)));
- extern void ShowNotice(const char *, ...) __attribute__((format(printf, 1, 2)));
- extern void ShowWarning(const char *, ...) __attribute__((format(printf, 1, 2)));
- extern void ShowDebug(const char *, ...) __attribute__((format(printf, 1, 2)));
- extern void ShowError(const char *, ...) __attribute__((format(printf, 1, 2)));
- extern void ShowFatalError(const char *, ...) __attribute__((format(printf, 1, 2)));
- extern void ShowConfigWarning(config_setting_t *config, const char *string, ...) __attribute__((format(printf, 2, 3)));
-#else
- HPExport void (*ShowMessage) (const char *, ...) __attribute__((format(printf, 1, 2)));
- HPExport void (*ShowStatus) (const char *, ...) __attribute__((format(printf, 1, 2)));
- HPExport void (*ShowSQL) (const char *, ...) __attribute__((format(printf, 1, 2)));
- HPExport void (*ShowInfo) (const char *, ...) __attribute__((format(printf, 1, 2)));
- HPExport void (*ShowNotice) (const char *, ...) __attribute__((format(printf, 1, 2)));
- HPExport void (*ShowWarning) (const char *, ...) __attribute__((format(printf, 1, 2)));
- HPExport void (*ShowDebug) (const char *, ...) __attribute__((format(printf, 1, 2)));
- HPExport void (*ShowError) (const char *, ...) __attribute__((format(printf, 1, 2)));
- HPExport void (*ShowFatalError) (const char *, ...) __attribute__((format(printf, 1, 2)));
+void showmsg_defaults(void);
#endif
#endif /* COMMON_SHOWMSG_H */