/* usb/lib/log.h
 *
 * (c) 2002 Simtec Electronics
 *
 * Ben Dooks
 *
 * USB Library: logging functions
 *
 * $Id: log.h,v 1.6 2003/08/06 15:56:11 ben Exp $
 *
 * This Library file is part of the Simtec Electronics USB stack development
 *   suite.
 * Specific licence is granted to use this file by third parties for the
 *   development of USB device drivers.
*/

#ifndef __USB_LIB_LOG_H
#define __USB_LIB_LOG_H "$Id: log.h,v 1.6 2003/08/06 15:56:11 ben Exp $"

#include "usb/sys/file.h"
#include "usb/sys/stdarg.h"
#include "usb/sys/log.h"

typedef struct usb_log_s *usb_log_t;
typedef struct usb_log_entry_s usb_log_entry_t;

struct usb_log_config_s {
  char     *name;
  int      max_entries;
  int      limit_max;
  int      entry_size;
};

struct usb_log_root_s {
  usb_log_entry_t          *list, *list_end;
  int                      count;

#ifdef USB_LOG_FILE
  FILE                     *file;
#endif
};

struct usb_log_sub_s {
 usb_log_t        root;
 usb_log_t        parent;
 int              prefix_length;
 char             *prefix;
};

enum usb_log_type_e {
  USB_LOG_TYPE_ROOT,
  USB_LOG_TYPE_SUB
};

typedef enum usb_log_type_e usb_log_type_t;

struct usb_log_s {
  usb_log_type_t            type;
  struct usb_log_config_s   config;

  union {
    struct usb_log_root_s   root;
    struct usb_log_sub_s    sub;
  } data;
};

struct usb_log_entry_s {
  usb_log_entry_t     *next;
  char                *msg;
};

/* definitions for usb log types */

#define USB_LOG_NORMAL (0x10000)
#define USB_LOG_ERR    (0x20000)
#define USB_LOG_WARN   (0x40000)

#define USB_LOG_NORM   (USB_LOG_NORMAL)

/* functions for clients to use */

/** creates a usb logging filehandle
@memo creates a usb logging endpoint
*/
extern usb_log_t usb_log_create(char *name,
                                int flags,
                                int entry_size,
                                int max_entries);

/** creates a usb sub logging filehandle
@memo creates a usb sub logging filehandle
*/
extern usb_log_t usb_log_create_sub(usb_log_t from,
                                    char *pfx,
                                    int flags,
                                    int entry_size,
                                    int max_entries);

/** frees a logging filehandle
 */
extern void usb_log_free(usb_log_t log);

/** restarts a log
 */
extern void usb_log_restart(usb_log_t log);

/** clears all memory resident entries from a log
 */
extern void usb_log_clear(usb_log_t log);

extern void usb_log_set(usb_log_t );

typedef void (*usb_log_print_fn)(usb_log_t log, const char *msg);

/** a function for usb_show_log to write current entries to the
 * log's file
*/
extern void usb_log_show_to_file(usb_log_t log, const char *msg);

/** send all currently memory buffered log entries to the supplied
 * routine
*/

extern void usb_show_log(usb_log_t log, usb_log_print_fn fn);

/** logs a formated string to a log file
 */
extern void usb_logf(usb_log_t to, int type, const char *msg, ...);

extern void usb_vlog(usb_log_t to, int type, const char *msg, va_list va);

/* stuff that is provided by system specific code */

extern int usb_log_getmaxentries(void);

#ifdef USB_LOG_FILE
extern char *usb_log_getname(char *buff, char *logname);
#endif


#endif /* __USB_LIB_LOG_H */
