Merge "Fix the definition of drm_virtgpu_execbuffer_with_ring_idx"
diff --git a/android-emu/android/utils/debug.c b/android-emu/android/utils/debug.c
index 27ad1d6..be166bb 100644
--- a/android-emu/android/utils/debug.c
+++ b/android-emu/android/utils/debug.c
@@ -1,134 +1,130 @@
-// Copyright 2007-2008 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
+/* Copyright (C) 2007-2008 The Android Open Source Project
+**
+** This software is licensed under the terms of the GNU General Public
+** License version 2, as published by the Free Software Foundation, and
+** may be copied, distributed, and modified under those terms.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+** GNU General Public License for more details.
+*/
 #include "android/utils/debug.h"
 
-#include <fcntl.h>
-#include <pthread.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <sys/syscall.h>
-#include <sys/types.h>
-#include <unistd.h>
+#include <fcntl.h>                 // for open, O_WRONLY
+#include <stdint.h>                // for uint64_t
+#include <stdio.h>                 // for fileno, fprintf, printf, stdout
+
+#ifdef _MSC_VER
+#include "msvc-posix.h"
+#else
+#include <sys/time.h>
+#include <time.h>                  // for localtime, tm, time_t
+#include <unistd.h>                // for dup2, close, dup
+#endif
+
+
+// TODO(jansene): Some external libraries (nibmle) still rely on these, so we cannot remove them yet.
+#undef dprint
+#undef dinfo
+#undef derror
+#undef dwarning
 
 uint64_t android_verbose = 0;
+LogSeverity android_log_severity = EMULATOR_LOG_INFO;
 
-void
-dprint( const char*  format,  ... )
-{
-    va_list  args;
-    va_start( args, format );
-    fprintf( stdout, "emulator: ");
-    vfprintf( stdout, format, args );
-    fprintf( stdout, "\n" );
-    va_end( args );
-}
-
-
-void
-dprintn( const char*  format, ... )
-{
-    va_list  args;
-    va_start( args, format );
-    vfprintf( stdout, format, args );
-    va_end( args );
-}
-
-void
-dprintnv( const char*  format, va_list args )
-{
-    vfprintf( stdout, format, args );
-}
-
-
-void
-dwarning( const char*  format, ... )
-{
-    va_list  args;
-    va_start( args, format );
-    dprintn( "emulator: WARNING: " );
-    dprintnv( format, args );
-    dprintn( "\n" );
-    va_end( args );
-}
-
-
-void
-derror( const char*  format, ... )
-{
-    va_list  args;
-    va_start( args, format );
-    dprintn( "emulator: ERROR: " );
-    dprintnv( format, args );
-    dprintn( "\n" );
-    va_end( args );
-}
-
-void
-android_tid_function_print(
-        bool use_emulator_prefix,
-        const char* function,
-        const char* format, ... )
-{
-    int tid = syscall(SYS_gettid);
+void dprint(const char* format, ...) {
     va_list args;
     va_start(args, format);
-    const char* prefix = use_emulator_prefix ? "emulator: " : "";
-    if (function) {
-        printf("%stid=0x%x: %s: ", prefix, tid,
-               function);
-    } else {
-        printf("%stid=0x%x: ", prefix, tid);
-    }
-
-    vprintf(format, args);
-    printf("\n");
+    fdprintfnv(stdout, 0, format, args);
     va_end(args);
 }
 
+void fdprintf(FILE* fp, const char* format, ...) {
+    va_list args;
+    va_start(args, format);
+    fdprintfnv(fp, 0, format, args);
+    va_end(args);
+}
+
+void fdprintfnv(FILE* fp, const char* lvl, const char* format, va_list args) {
+    if (VERBOSE_CHECK(time)) {
+        struct timeval tv;
+        gettimeofday(&tv, 0);
+        time_t now = tv.tv_sec;
+        struct tm* time = localtime(&now);
+        fprintf(fp, "%02d:%02d:%02d.%05ld ", time->tm_hour, time->tm_min,
+                time->tm_sec, tv.tv_usec);
+    }
+    fprintf(fp, "emulator: ");
+    if (lvl) {
+        fprintf(fp, "%s", lvl);
+    }
+    vfprintf(fp, format, args);
+    fprintf(fp, "\n");
+}
+
+void dprintn(const char* format, ...) {
+    va_list args;
+    va_start(args, format);
+    vfprintf(stdout, format, args);
+    va_end(args);
+}
+
+void dprintnv(const char* format, va_list args) {
+    vfprintf(stdout, format, args);
+}
+void dinfo(const char* format, ...) {
+    va_list args;
+    va_start(args, format);
+    fdprintfnv(stdout, "INFO: ", format, args);
+    va_end(args);
+}
+
+void dwarning(const char* format, ...) {
+    va_list args;
+    va_start(args, format);
+    fdprintfnv(stdout, "WARNING: ", format, args);
+    va_end(args);
+}
+
+void derror(const char* format, ...) {
+    va_list args;
+    va_start(args, format);
+    fdprintfnv(stdout, "ERROR: ", format, args);
+    va_end(args);
+}
+
+
 /** STDOUT/STDERR REDIRECTION
  **
  ** allows you to shut temporarily shutdown stdout/stderr
  ** this is useful to get rid of debug messages from ALSA and esd
  ** on Linux.
  **/
-static int    stdio_disable_count;
-static int    stdio_save_out_fd;
-static int    stdio_save_err_fd;
+static int stdio_disable_count;
+static int stdio_save_out_fd;
+static int stdio_save_err_fd;
 
 #ifdef _WIN32
-extern void
-stdio_disable( void )
-{
+extern void stdio_disable(void) {
     if (++stdio_disable_count == 1) {
-        int  null_fd, out_fd, err_fd;
+        int null_fd, out_fd, err_fd;
         fflush(stdout);
         out_fd = _fileno(stdout);
         err_fd = _fileno(stderr);
         stdio_save_out_fd = _dup(out_fd);
         stdio_save_err_fd = _dup(err_fd);
-        null_fd = _open( "NUL", _O_WRONLY );
+        null_fd = _open("NUL", _O_WRONLY);
         _dup2(null_fd, out_fd);
         _dup2(null_fd, err_fd);
         close(null_fd);
     }
 }
 
-extern void
-stdio_enable( void )
-{
+extern void stdio_enable(void) {
     if (--stdio_disable_count == 0) {
-        int  out_fd, err_fd;
+        int out_fd, err_fd;
         fflush(stdout);
         out_fd = _fileno(stdout);
         err_fd = _fileno(stderr);
@@ -139,28 +135,24 @@
     }
 }
 #else
-extern void
-stdio_disable( void )
-{
+extern void stdio_disable(void) {
     if (++stdio_disable_count == 1) {
-        int  null_fd, out_fd, err_fd;
+        int null_fd, out_fd, err_fd;
         fflush(stdout);
         out_fd = fileno(stdout);
         err_fd = fileno(stderr);
         stdio_save_out_fd = dup(out_fd);
         stdio_save_err_fd = dup(err_fd);
-        null_fd = open( "/dev/null", O_WRONLY );
+        null_fd = open("/dev/null", O_WRONLY);
         dup2(null_fd, out_fd);
         dup2(null_fd, err_fd);
         close(null_fd);
     }
 }
 
-extern void
-stdio_enable( void )
-{
+extern void stdio_enable(void) {
     if (--stdio_disable_count == 0) {
-        int  out_fd, err_fd;
+        int out_fd, err_fd;
         fflush(stdout);
         out_fd = fileno(stdout);
         err_fd = fileno(stderr);
diff --git a/android-emu/android/utils/debug.h b/android-emu/android/utils/debug.h
index be1c1b2..3de53b0 100644
--- a/android-emu/android/utils/debug.h
+++ b/android-emu/android/utils/debug.h
@@ -1,24 +1,24 @@
-// Copyright 2007-2008 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
+/* Copyright (C) 2007-2008 The Android Open Source Project
+**
+** This software is licensed under the terms of the GNU General Public
+** License version 2, as published by the Free Software Foundation, and
+** may be copied, distributed, and modified under those terms.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+** GNU General Public License for more details.
+*/
 
 #pragma once
 
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdint.h>
+#include <stdio.h>
 
 #include "android/utils/compiler.h"
+#include "android/utils/log_severity.h"
 
 ANDROID_BEGIN_HEADER
 
@@ -69,90 +69,86 @@
     _VERBOSE_TAG(virtualscene, "Virtual scene rendering")                      \
     _VERBOSE_TAG(automation, "Automation")                                     \
     _VERBOSE_TAG(offworld, "Offworld")                                         \
+    _VERBOSE_TAG(videoinjection, "Video injection")                            \
+    _VERBOSE_TAG(foldable, "Foldable Device")                                  \
+    _VERBOSE_TAG(curl, "Libcurl requests")                                     \
+    _VERBOSE_TAG(car_rotary, "Car rotary controller")                          \
+    _VERBOSE_TAG(wifi, "Virtio Wifi")                                          \
+    _VERBOSE_TAG(tvremote, "TV remote")                                        \
+    _VERBOSE_TAG(time, "Prefix a timestamp when logging")                      \
+    _VERBOSE_TAG(ini, "Log details around ini files.")                         \
+    _VERBOSE_TAG(bluetooth, "Log bluetooth details.")                          \
+    _VERBOSE_TAG(log, "Include timestamp, thread and location in logs")
 
-#define  _VERBOSE_TAG(x,y)  VERBOSE_##x,
+#define _VERBOSE_TAG(x, y) VERBOSE_##x,
 typedef enum {
-    VERBOSE_TAG_LIST
-    VERBOSE_MAX  /* do not remove */
+    VERBOSE_TAG_LIST VERBOSE_MAX /* do not remove */
 } VerboseTag;
-#undef  _VERBOSE_TAG
+#undef _VERBOSE_TAG
 
 extern uint64_t android_verbose;
 
 // Enable/disable verbose logs from the base/* family.
 extern void base_enable_verbose_logs();
 extern void base_disable_verbose_logs();
+#define VERBOSE_ENABLE(tag) android_verbose |= (1ULL << VERBOSE_##tag)
 
-#define  VERBOSE_ENABLE(tag)    \
-    android_verbose |= (1ULL << VERBOSE_##tag)
+#define VERBOSE_DISABLE(tag) android_verbose &= (1ULL << VERBOSE_##tag)
 
-#define  VERBOSE_DISABLE(tag)   \
-    android_verbose &= (1ULL << VERBOSE_##tag)
+#define VERBOSE_CHECK(tag) ((android_verbose & (1ULL << VERBOSE_##tag)) != 0)
 
-#define  VERBOSE_CHECK(tag)    \
-    ((android_verbose & (1ULL << VERBOSE_##tag)) != 0)
+#define VERBOSE_CHECK_ANY() (android_verbose != 0)
 
-#define  VERBOSE_CHECK_ANY()    \
-    (android_verbose != 0)
+extern void __emu_log_print(LogSeverity prio,
+                            const char* file,
+                            int line,
+                            const char* fmt,
+                            ...);
 
-#define  VERBOSE_PRINT(tag,...)  \
-    do { if (VERBOSE_CHECK(tag)) dprint(__VA_ARGS__); } while (0)
+#ifndef EMULOG
+#define EMULOG(priority, fmt, ...) \
+    __emu_log_print(priority, __FILE__, __LINE__, fmt, ##__VA_ARGS__);
+#endif
 
-// This omits the "emulator: " prefix.
-#define  VERBOSE_DPRINT(tag,format,...)  \
-    do { if (VERBOSE_CHECK(tag)) { \
-         dprintn(format "\n", \
-                 ##__VA_ARGS__); } } while(0)
+#define VERBOSE_PRINT(tag, ...)                      \
+    if (VERBOSE_CHECK(tag)) {                        \
+        EMULOG(EMULATOR_LOG_VERBOSE, ##__VA_ARGS__); \
+    }
 
-#define  VERBOSE_FUNCTION_PRINT(tag,format,...)  \
-    do { if (VERBOSE_CHECK(tag)) \
-         dprintn("emulator: %s: " format "\n", \
-                 __func__, ##__VA_ARGS__); } while(0)
+#define VERBOSE_INFO(tag, ...)                    \
+    if (VERBOSE_CHECK(tag)) {                     \
+        EMULOG(EMULATOR_LOG_INFO, ##__VA_ARGS__); \
+    }
 
-// This omits the "emulator: " prefix.
-#define  VERBOSE_FUNCTION_DPRINT(tag,format,...)  \
-    do { if (VERBOSE_CHECK(tag)) \
-         dprintn("%s: " format "\n", \
-                 __func__, ##__VA_ARGS__); } while(0)
+#define VERBOSE_DPRINT(tag, ...) VERBOSE_PRINT(tag, __VA_ARGS__)
 
-#define  VERBOSE_TID_PRINT(tag,...)  \
-    do { if (VERBOSE_CHECK(tag)) \
-         android_tid_function_print(true, NULL, __VA_ARGS__); \
-    } while (0)
+extern void dprintn(const char* format, ...);
 
-// This omits the "emulator: " prefix.
-#define  VERBOSE_TID_DPRINT(tag,...)  \
-    do { if (VERBOSE_CHECK(tag)) \
-         android_tid_function_print(false, NULL, __VA_ARGS__); } \
-    while (0)
+extern void fdprintfnv(FILE* fp,
+                       const char* level,
+                       const char* format,
+                       va_list args);
 
-#define  VERBOSE_TID_FUNCTION_PRINT(tag,...) \
-    do { if (VERBOSE_CHECK(tag)) \
-         android_tid_function_print(true, __func__, __VA_ARGS__); } \
-    while (0)
+// Logging support.
 
-// This omits the "emulator: " prefix.
-#define  VERBOSE_TID_FUNCTION_DPRINT(tag,...) \
-    do { if (VERBOSE_CHECK(tag)) \
-         android_tid_function_print(false, __func__, __VA_ARGS__); } \
-    while (0)
+#define dprint(fmt, ...)                                 \
+    if (EMULATOR_LOG_VERBOSE >= android_log_severity) {  \
+        EMULOG(EMULATOR_LOG_VERBOSE, fmt, ##__VA_ARGS__) \
+    }
 
-/** DEBUG TRACE SUPPORT
- **
- ** Debug messages can be sent by calling these functions:
- **
- ** 'dprint' prints "emulator: ", the message, then appends a '\n'
- ** 'dprintn' prints the message as is
- ** 'dprintnv' is 'dprintn' but allows you to use a va_list argument
- ** 'dwarning' prints "emulator: WARNING: ", then appends a '\n'
- ** 'derror' prints "emulator: ERROR: ", then appends a '\n'
- */
-
-extern void   dprint( const char*  format, ... );
-extern void   dprintn( const char*  format, ... );
-extern void   dprintnv( const char*  format, va_list  args );
-extern void   dwarning( const char*  format, ... );
-extern void   derror( const char*  format, ... );
+#define dinfo(fmt, ...)                               \
+    if (EMULATOR_LOG_INFO >= android_log_severity) {  \
+        EMULOG(EMULATOR_LOG_INFO, fmt, ##__VA_ARGS__) \
+    }
+#define dwarning(fmt, ...)                               \
+    if (EMULATOR_LOG_WARNING >= android_log_severity) {  \
+        EMULOG(EMULATOR_LOG_WARNING, fmt, ##__VA_ARGS__) \
+    }
+#define derror(fmt, ...)                               \
+    if (EMULATOR_LOG_ERROR >= android_log_severity) {  \
+        EMULOG(EMULATOR_LOG_ERROR, fmt, ##__VA_ARGS__) \
+    }
+#define dfatal(fmt, ...) EMULOG(EMULATOR_LOG_FATAL, fmt, ##__VA_ARGS__)
 
 /** MULTITHREADED DEBUG TRACING
  **
@@ -160,9 +156,10 @@
  ** It prints "emulator: " or not (depending on |use_emulator_prefix|),
  ** the thread id, a function name (|function|), the message, and finally '\n'.
  */
-extern void   android_tid_function_print(bool use_emulator_prefix,
-                                         const char* function,
-                                         const char*  format, ... );
+extern void android_tid_function_print(bool use_emulator_prefix,
+                                       const char* function,
+                                       const char* format,
+                                       ...);
 
 /** STDOUT/STDERR REDIRECTION
  **
@@ -171,7 +168,7 @@
  ** on Linux.
  **/
 
-extern void  stdio_disable( void );
-extern void  stdio_enable( void );
+extern void stdio_disable(void);
+extern void stdio_enable(void);
 
 ANDROID_END_HEADER
diff --git a/android-emu/android/utils/log_severity.h b/android-emu/android/utils/log_severity.h
new file mode 100644
index 0000000..2b27f69
--- /dev/null
+++ b/android-emu/android/utils/log_severity.h
@@ -0,0 +1,42 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+#pragma once
+#include "android/utils/compiler.h"
+#include "android/utils/log_severity.h"
+
+ANDROID_BEGIN_HEADER
+
+// Defines the available log severities.
+
+typedef enum LogSeverity {
+    EMULATOR_LOG_DEBUG = -2,
+    EMULATOR_LOG_VERBOSE = -1,
+    EMULATOR_LOG_INFO = 0,
+    EMULATOR_LOG_WARNING = 1,
+    EMULATOR_LOG_ERROR = 2,
+    EMULATOR_LOG_FATAL = 3,
+    EMULATOR_LOG_NUM_SEVERITIES,
+
+// DFATAL will be ERROR in release builds, and FATAL in debug ones.
+#ifdef NDEBUG
+    EMULATOR_LOG_DFATAL = EMULATOR_LOG_ERROR,
+#else
+    EMULATOR_LOG_DFATAL = EMULATOR_LOG_FATAL,
+#endif
+} LogSeverity;
+
+extern LogSeverity android_log_severity;
+// #endif
+
+ANDROID_END_HEADER